标签 curl 下的文章

http_build_query这个函数可以很方便的构造一个请求所需要的参数。(不分GET,POST)

这个函数可以把一个数组转化为一串字符

栗子1:

'bar',
              'baz'=>'boom',
              'cow'=>'milk',
              'php'=>'hypertext processor');
 
//第二个参数是分割使用的符号
echo http_build_query($data) . "\n";
echo http_build_query($data, '', '&');
# foo=bar&baz=boom&cow=milk&php=hypertext+processor
# foo=bar&baz=boom&cow=milk&php=hypertext+processor
?>

栗子二:

array('name'=>'Bob Smith',
                            'age'=>47,
                            'sex'=>'M',
                            'dob'=>'5/12/1956'),
              'pastimes'=>array('golf', 'opera', 'poker', 'rap'),
              'children'=>array('bobby'=>array('age'=>12,
                                               'sex'=>'M'),
                                'sally'=>array('age'=>8,
                                               'sex'=>'F')),
              'CEO');
//第二个参数,如果数组没有键值,则自动添加键值
echo http_build_query($data, 'flags_');
?>

输出:

user%5Bname%5D=Bob+Smith&user%5Bage%5D=47&user%5Bsex%5D=M&user%5Bdob%5D=5%2F12%2F1956&pastimes%5B0%5D=golf&pastimes%5B1%5D=opera&pastimes%5B2%5D=poker&pastimes%5B3%5D=rap&children%5Bbobby%5D%5Bage%5D=12&children%5Bbobby%5D%5Bsex%5D=M&children%5Bsally%5D%5Bage%5D=8&children%5Bsally%5D%5Bsex%5D=F&flags_0=CEO

把上面的请求传给一个PHP文件处理,打印传递来的参数,会得到以下结果:

Array
(
    [_GET] => Array
        (
            [user] => Array
                (
                    [name] => Bob Smith
                    [age] => 47
                    [sex] => M
                    [dob] => 5/12/1956
                )
 
            [pastimes] => Array
                (
                    [0] => golf
                    [1] => opera
                    [2] => poker
                    [3] => rap
                )
 
            [children] => Array
                (
                    [bobby] => Array
                        (
                            [age] => 12
                            [sex] => M
                        )
 
                    [sally] => Array
                        (
                            [age] => 8
                            [sex] => F
                        )
 
                )
 
            [flags_0] => CEO
        )
 
    [_POST] => Array
        (
        )
 
    [_COOKIE] => Array
        (
            [PHPSESSID] => 5u7l53inhrl5j1ojmn65ok4k44
        )
 
    [_FILES] => Array
        (
        )
 
    [GLOBALS] => Array
 *RECURSION*
)

很神奇吧!

栗子三:(构造HTTP请求)

 'some content',
        'var2' => 'doh'
    )
);
 
$opts = array('http' =>
    array(
        'method'  => 'POST',
        'header'  => 'Content-type: application/x-www-form-urlencoded',
        'content' => $postdata
    )
);
 
$context = stream_context_create($opts);
 
$result = file_get_contents('http://example.com/submit.php', false, $context);
 
?>

这些函数配合起来就可以用file_get_contents构造出一个POST或GET请求了,比CURL方便很多!

这个是采集基础,最好熟悉一下

$ch = curl_init();
# 设定url和把结果返回,是否返回头部
curl_setopt($ch, CURLOPT_URL, 'http://www.baidu.com/');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($this->ch, CURLOPT_HEADER, 1);

# cookie文件设定
curl_setopt($this->ch, CURLOPT_COOKIEJAR,  $cookie_file);
curl_setopt($this->ch, CURLOPT_COOKIEFILE, $cookie_file);

# 额外头部
curl_setopt($this->ch, CURLOPT_HTTPHEADER, array('User-Agent: Mozilla/5.0'));

# 设定post
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $poststring);

# 连接、执行过期时间
curl_setopt($this->ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($this->ch, CURLOPT_TIMEOUT, 30);

# 是否跟随301 302
curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($this->ch, CURLOPT_MAXREDIRS, 10);

# refer
curl_setopt($this->ch, CURLOPT_REFERER, $refer);

# http版本和端口重用设置
curl_setopt($this->ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($this->ch, CURLOPT_FORBID_REUSE, 1);

# 支持https
curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($this->ch, CURLOPT_SSL_VERIFYHOST, 0);

# 如果需要进行毫秒超时,需要增加:
curl_setopt($this->ch, CURLOPT_NOSIGNAL, 1);

# 执行
$response = curl_exec($ch);
if(curl_errno($ch)){
    curl_error($ch);
    exit();
}
curl_close($ch);