简体   繁体   English

PHP无法从URL获取文本(file_get_content,CURL不起作用)

[英]PHP Cannot get the text from an URL (file_get_content, CURL did not work)

I am trying to get the text from the following url. 我试图从以下网址获取文本。 http://stp.stheadline.com/data/indexNewsMarquee.json http://stp.stheadline.com/data/indexNewsMarquee.json

I have tried several methods but none of them worked. 我尝试过几种方法,但没有一种方法可行。 I am really desperate right now please help me and thanks in advance. 我现在非常绝望,请帮助我,并提前感谢。

PS I have enabled allow_url_fopen in my ini.php already. PS我已经在我的ini.php中启用了allow_url_fopen PS I am using XAMPP v3.3.2 and PHP v5.6.23 PS我正在使用XAMPP v3.3.2和PHP v5.6.23

the following are the codes that I have tried (and failed) 以下是我尝试过的代码(并且失败了)

CURL 卷曲

$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: Empty string 结果: 空字符串

file_get_contents 的file_get_contents

echo file_get_contents($url, true);

result: failed to open stream: HTTP request failed! 结果: 无法打开流:HTTP请求失败!

readfile ReadFile的

echo readfile($url)

result: output the address itself 结果: 输出地址本身

They are requiring a useragent to be present in the request, so try: 他们要求使用者出现在请求中,所以请尝试:

$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;

Hi you have to pass header in your curl request, so server can treat its as browser request. 您好,您必须在您的curl请求中传递标题,因此服务器可以将其视为浏览器请求。

 $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;

Use Just like this: 使用就像这样:

 $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);

I checked this Code from Your URL. 我从您的URL检查了此代码。

Try this out. 试试吧。

$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);*/

Or 要么

$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

It's working for me :) 它对我有用:)

Kevin, 凯文,

Please try below Code. 请尝试以下代码。

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);

I have added header('Content-Type: text/html; charset=utf-8'); 我添加了header('Content-Type: text/html; charset=utf-8'); and curl_setopt($ch, CURLOPT_ENCODING, ""); curl_setopt($ch, CURLOPT_ENCODING, ""); in your code, and it will work well, also will display correct characters as well. 在你的代码中,它将运行良好,也将显示正确的字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM