封装好的CURL和Fsockopen函数

最近在解决项目中一个问题时用到了fsockopen,真是不可小觑。在查相关资料时又见识到了CURL的强大威力,下面转载了两个封装好的函数,以备后用。源地址


<?
//CURL
/**
* 使用:
* echo cevin_http_open('http://www.baidu.com');
*
* POST数据
* $post = array('aa'=>'ddd','ee'=>'d')
* 或
* $post = 'aa=ddd&ee=d';
* echo cevin_http_open('http://www.baidu.com',array('post'=>$post));
*/
function cevin_http_open($url, $conf = array())
{
    if(!function_exists('curl_init') or !is_array($conf))  return FALSE;   

    $post = '';
    $purl = parse_url($url);   

    $arr = array(
        'post' => FALSE,
        'return' => TRUE,
        'cookie' => 'C:/cookie.txt',);
    $arr = array_merge($arr, $conf);
    $ch = curl_init();   

    if($purl['scheme'] == 'https')
    {
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    }   

    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, $arr['return']);
    curl_setopt($ch, CURLOPT_COOKIEJAR, $arr['cookie']);
    curl_setopt($ch, CURLOPT_COOKIEFILE, $arr['cookie']);   

    if($arr['post'] != FALSE)
    {
        curl_setopt($ch, CURL_POST, TRUE);
        if(is_array($arr['post']))
        {
            $post = http_build_query($arr['post']);
        } else {
            $post = $arr['post'];
        }
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
    }   

    $result = curl_exec($ch);
    curl_close($ch);   

    return $result;
}

//Fsockopen
/**
*使用方法同CURL
*/
function sw_http_open($url, $conf = array()) {
    $return = '';
    if(!is_array($conf))
    {
        return $return;
    }
    $matches = parse_url($url);
    !isset($matches['host']) && $matches['host'] = '';
    !isset($matches['path']) && $matches['path'] = '';
    !isset($matches['query']) && $matches['query'] = '';
    !isset($matches['port']) && $matches['port'] = '';
    $host = $matches['host'];
    $path = $matches['path'] ? $matches['path'].($matches['query'] ? '?'.$matches['query'] : '') : '/';
    $port = !emptyempty($matches['port']) ? $matches['port'] : 80;   

    $conf_arr = array(
        'limit'=>0,
        'post'=>'',
        'cookie'=>'',
        'bysocket'=>FALSE,
        'ip'=>'',
        'timeout'=>15,
        'block'=>TRUE,
        );   

    foreach (array_merge($conf_arr, $conf) as $k=>$v) ${$k} = $v;   

    if($post) {
        if(is_array($post))
        {
            $post = http_build_query($post);
        }
        $out = "POST $path HTTP/1.0rn";
        $out .= "Accept: */*rn";
        //$out .= "Referer: $boardurlrn";
        $out .= "Accept-Language: zh-cnrn";
        $out .= "Content-Type: application/x-www-form-urlencodedrn";
        $out .= "User-Agent: $_SERVER[HTTP_USER_AGENT]rn";
        $out .= "Host: $hostrn";
        $out .= 'Content-Length: '.strlen($post)."rn";
        $out .= "Connection: Closern";
        $out .= "Cache-Control: no-cachern";
        $out .= "Cookie: $cookiernrn";
        $out .= $post;
    } else {
        $out = "GET $path HTTP/1.0rn";
        $out .= "Accept: */*rn";
        //$out .= "Referer: $boardurlrn";
        $out .= "Accept-Language: zh-cnrn";
        $out .= "User-Agent: $_SERVER[HTTP_USER_AGENT]rn";
        $out .= "Host: $hostrn";
        $out .= "Connection: Closern";
        $out .= "Cookie: $cookiernrn";
    }
    $fp = @fsockopen(($ip ? $ip : $host), $port, $errno, $errstr, $timeout);
    if(!$fp) {
        return '';
    } else {
        stream_set_blocking($fp, $block);
        stream_set_timeout($fp, $timeout);
        @fwrite($fp, $out);
        $status = stream_get_meta_data($fp);
        if(!$status['timed_out']) {
            while (!feof($fp)) {
                if(($header = @fgets($fp)) && ($header == "rn" ||  $header == "n")) {
                    break;
                }
            }   

            $stop = false;
            while(!feof($fp) && !$stop) {
                $data = fread($fp, ($limit == 0 || $limit > 8192 ? 8192 : $limit));
                $return .= $data;
                if($limit) {
                    $limit -= strlen($data);
                    $stop = $limit <= 0;
                }
            }
        }
        @fclose($fp);
        return $return;
    }
}
?>


( ! ) Warning: Missing argument 1 for cwppos_show_review(), called in /data/www/enjoyphp/wp-content/themes/flat/content-single.php on line 29 and defined in /data/www/enjoyphp/wp-content/plugins/wp-product-review/includes/legacy.php on line 18
Call Stack
#TimeMemoryFunctionLocation
10.0000354856{main}( ).../index.php:0
20.0001355144require( '/data/www/enjoyphp/wp-blog-header.php' ).../index.php:17
30.198410166800require_once( '/data/www/enjoyphp/wp-includes/template-loader.php' ).../wp-blog-header.php:19
40.199610237944include( '/data/www/enjoyphp/wp-content/themes/flat/single.php' ).../template-loader.php:74
50.282210817408get_template_part( ).../single.php:5
60.282210817928locate_template( ).../general-template.php:167
70.282310818024load_template( ).../template.php:643
80.282310818408require( '/data/www/enjoyphp/wp-content/themes/flat/content-single.php' ).../template.php:686
90.287310803536cwppos_show_review( ).../content-single.php:29

发表评论

电子邮件地址不会被公开。 必填项已用*标注