我想通過使用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數據? –