我试图从以下url获取文本。http://stp.stheadline.com/data/indexNewsMarquee.json
我试过几种方法,但都不起作用。我现在真的走投无路了,请帮帮我,提前谢谢你。
注:我已经在我的ini.php中启用了allow_url_fopen。P.S.我使用XAMPP v3.3.2和PHP v5.6.23
以下是我尝试过的代码(都失败了)
旋度$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $url);
$result = curl_exec($ch);
curl_close($ch);
echo $result;
result: 空字符串
<标题> file_get_contents h1> esult: failed to open stream: HTTP请求失败!<标题> readfile h1> esult: 输出地址本身标题>标题>你好,你必须在你的curl请求中传递header,以便服务器可以将其视为浏览器请求。
$url="http://stp.stheadline.com/data/indexNewsMarquee.json";
$requestHeaders = array(
"Accept:application/json, text/javascript, */*; q=0.01",
"Accept-Language:en-US,en;q=0.8",
"Connection:keep-alive",
"Host:stp.stheadline.com",
"Origin:http://stp.stheadline.com",
"Referer:http://stp.stheadline.com/data/indexNewsMarquee.json",
"User-Agent:Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36"
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $requestHeaders);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
echo $output;
它们要求在请求中存在一个用户代理,所以尝试:
$url='http://stp.stheadline.com/data/indexNewsMarquee.json';
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'banana');
curl_setopt($ch, CURLOPT_URL, $url);
$result = curl_exec($ch);
curl_close($ch);
echo $result;
使用如下:
$curl_handle=curl_init();
curl_setopt($curl_handle,CURLOPT_URL,'Your URL');
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handle, CURLOPT_USERAGENT, 'Your application name');
$query = curl_exec($curl_handle);
curl_close($curl_handle);
print_r($query);
我从你的URL检查了这个代码。
Kevin
请尝试下面的代码。
header('Content-Type: text/html; charset=utf-8');
$url='http://stp.stheadline.com/data/indexNewsMarquee.json';
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array('text/html; charset=utf-8'));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_ENCODING, "");
curl_setopt($ch, CURLOPT_URL, $url);
$result = curl_exec($ch);
curl_close($ch);
$final_result = json_decode($result);
echo "<pre>";
print_r($final_result);
我在你的代码中添加了header('Content-Type: text/html; charset=utf-8');
和curl_setopt($ch, CURLOPT_ENCODING, "");
,它会工作得很好,也会显示正确的字符。
试试这个。
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, 'http://stp.stheadline.com/data/indexNewsMarquee.json');
$result = curl_exec($ch);
curl_close($ch);
echo($result); // Json data
/*$obj = json_decode($result); // To get result in array
print_r($obj);*/
或
$url = 'http://stp.stheadline.com/data/indexNewsMarquee.json';
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $url);
$result = curl_exec($ch);
curl_close($ch);
echo($result); // Json data
这对我来说是有效的