我想通过使用curl摆脱bseindia.com数据,但我只得到空数据..PHP的file_get_contents或卷曲不工作
这里是我的卷曲功能代码:
<?php
function load($url, $ispost=0, $data='', $header=array()){
if($url=='' || ($ispost && $data=='')) return;
$host=parse_url($url, PHP_URL_HOST);
$header[]='User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:6.0) Gecko/20100101 Firefox/6.0';
$header[]='Host: '.$host;
$header[]='Connection: keep-alive';
$header[]='Referer: http://'.$host.'/';
if($ispost) $header[]='Content-length: '.strlen($data);
$ch = curl_init("$url");
if($ispost){
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
$file = curl_exec($ch);
$header = curl_getinfo($ch);
curl_close($ch);
return array('http_code'=>$header['http_code'], 'output'=>$file);
}
?>
和这里的我的电话:
<?php
$data = load('http://www.bseindia.com/', 0, '');
if($data['http_code']!==200) exit(json_encode(array('err'=>true, 'msg'=>'Unable to get! Error='.$data['http_code'])));
exit(json_encode(array('err'=>false, 'msg'=>json_decode($data['output']))));
?>
,但我在JSON输出是:
{"err":false,"msg":null}
但如果我改变
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
到
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
然后我正在直接印刷,但不被存储在变量输出,为什么?
所以,我的问题是如何获取数据,而无需使用:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
???
是否有可能与您关闭标题('CURLOPT_HEADER,0'),但仍然要求它们('curl_getinfo')有关? – OverZealous
@OverZealous:但它的工作其他网站,如:google.com ... –
似乎'json_decode($ data ['output'])'返回NULL。该网站是否返回有效的* JSON数据? –