php获取数据为啥curl获取不完整

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php获取数据为啥curl获取不完整相关的知识,希望对你有一定的参考价值。

因为,php CURL库默认1024字节的长度不等待数据的返回,所以你那段代码需增加一项配置:

curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
给你一个更全面的封装方法:

function req_curl($url, &$status = null, $options = array())

$res = '';
$options = array_merge(array(
'follow_local' => true,
'timeout' => 30,
'max_redirects' => 4,
'binary_transfer' => false,
'include_header' => false,
'no_body' => false,
'cookie_location' => dirname(__FILE__) . '/cookie',
'useragent' => 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1',
'post' => array() ,
'referer' => null,
'ssl_verifypeer' => 0,
'ssl_verifyhost' => 0,
'headers' => array(
'Expect:'
) ,
'auth_name' => '',
'auth_pass' => '',
'session' => false
) , $options);
$options['url'] = $url;
$s = curl_init();
if (!$s) return false;
curl_setopt($s, CURLOPT_URL, $options['url']);
curl_setopt($s, CURLOPT_HTTPHEADER, $options['headers']);
curl_setopt($s, CURLOPT_SSL_VERIFYPEER, $options['ssl_verifypeer']);
curl_setopt($s, CURLOPT_SSL_VERIFYHOST, $options['ssl_verifyhost']);
curl_setopt($s, CURLOPT_TIMEOUT, $options['timeout']);
curl_setopt($s, CURLOPT_MAXREDIRS, $options['max_redirects']);
curl_setopt($s, CURLOPT_RETURNTRANSFER, true);
curl_setopt($s, CURLOPT_FOLLOWLOCATION, $options['follow_local']);
curl_setopt($s, CURLOPT_COOKIEJAR, $options['cookie_location']);
curl_setopt($s, CURLOPT_COOKIEFILE, $options['cookie_location']);
if (!empty($options['auth_name']) && is_string($options['auth_name']))

curl_setopt($s, CURLOPT_USERPWD, $options['auth_name'] . ':' . $options['auth_pass']);

if (!empty($options['post']))

curl_setopt($s, CURLOPT_POST, true);
curl_setopt($s, CURLOPT_POSTFIELDS, $options['post']);
//curl_setopt($s, CURLOPT_POSTFIELDS, array('username' => 'aeon', 'password' => '111111'));

if ($options['include_header'])

curl_setopt($s, CURLOPT_HEADER, true);

if ($options['no_body'])

curl_setopt($s, CURLOPT_NOBODY, true);

if ($options['session'])

curl_setopt($s, CURLOPT_COOKIESESSION, true);
curl_setopt($s, CURLOPT_COOKIE, $options['session']);

curl_setopt($s, CURLOPT_USERAGENT, $options['useragent']);
curl_setopt($s, CURLOPT_REFERER, $options['referer']);
$res = curl_exec($s);
$status = curl_getinfo($s, CURLINFO_HTTP_CODE);
curl_close($s);
return $res;
参考技术A 我只能说你的curl手册没有看认真,CURLOPT_HTTPHEADER这个选项的用法肯定是按你自己想象的来的,实际上并不是这样,应该
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Accept: */*',
'Accept-Charset: UTF-8,*;q=0.5',
'Accept-Encoding: gzip,deflate,sdch',
'Accept-Language: zh-CN,zh;q=0.8',
'Connection: keep-alive',
'Content-Type: application/x-www-form-urlencoded; charset=UTF-8',
'Referer: http://fuzhou.8684.cn/',
'User-Agent: Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.11 (Khtml, like Gecko) Chrome/23.0.1271.95 Safari/537.11',
'X-Requested-With: XMLHttpRequest',
));

PHP 使用cURL PHP从URL获取数据

<?php
$url='http://twitter.com/statuses/user_timeline/16387631.json'; //rss link for the twitter timeline
print_r(get_data($url)); //dumps the content, you can manipulate as you wish to
 
/* gets the data from a URL */
 
function get_data($url)
{
$ch = curl_init();
$timeout = 5;
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
?>

以上是关于php获取数据为啥curl获取不完整的主要内容,如果未能解决你的问题,请参考以下文章

php的curl如何获取一个跳转页面后的数据啊

PHP 使用cURL PHP从URL获取数据

PHP通过CURL模拟登录并获取数据

PHP模拟登录并获取数据

php的curl获取https加密协议请求返回json数据进行信息获取

php curl没有从youtube API获取数据[重复]