Kevin Kevin - 3 months ago 14
HTTP Question

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

I have tried several methods but none of them worked. I am really desperate right now please help me and thanks in advance.

P.S. I have enabled allow_url_fopen in my ini.php already.
P.S. I am using XAMPP v3.3.2 and 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



echo file_get_contents($url, true);


result: failed to open stream: HTTP request failed!

readfile



echo readfile($url)


result: output the address itself

Answer

Hi you have to pass header in your curl request, so server can treat its as browser request.

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