4

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

1

5 Answers 5

2

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;
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks it works. can you explain a bit what I did wrong (what does it mean to pass the header) I am quite new to this so I would really appreciate it if you can elaborate a little.Thanks in advance
Yeah @Kevin, it seems stp.stheadline.com has implemented security for prevent access unkown requests on server. By including headers in curl request like origin, referer, urger agent.. so that server ensures that request come from browser... it like some tricks to use CURL request as browser request :).. I hope you like my answer and give up vote and mark as correct... thanks in advance
thanks for your explanation. How can determine if the website has the security implementation. Just curious. Thank you.
2

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;

3 Comments

I'm genuinely curios how'd you figure out the need useragent in the request...?
Experience of trying curl requests has shown that on many occasions when a request has failed silently that adding a useragent makes it work ( as it did in this request ) ~ presumably because some bots do not have a useragent and thus do not appear as valid browser traffic.
Huh, well. Good to know.
0

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.

3 Comments

and Leave Section of "Your Application Name" and tried out
Thanks it works. In your code, there are (CURLOPT_USERAGENT and **CONNECTION_TIMEOUT**(which I dont have). Can you tell me what the code did? Thanks in advance.
@Kevin 1. CONNECTTIMEOUT : The number of seconds to wait while trying to connect. (0 For infinite). 2. USERAGENT : The contents of the "User-Agent: " header to be used in a HTTP request.
0

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'); and curl_setopt($ch, CURLOPT_ENCODING, ""); in your code, and it will work well, also will display correct characters as well.

Comments

0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.