0

In the bellow snipet you can see that i am trying to download some data from a website via their API in JSON format.

My problem is that when i try to get the data in json format (25 mb of text data) the php script send me a 200 response (which you can see below). But the weird part is that the script actualy finishes executing and the response is downloaded by my browser as a file.

The url if inputed into browser returns correct data, even via wget i can download the contents from this url.

Could it be that curl will pass through the respose from the server to my browser?

$ch = curl_init("https://example.com/api/json/view/view_name?authtoken=xxxyyyaaaa&raw=true");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$data = curl_exec ($ch);

downloaded file "myscriptname.php" which contains:

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>200 OK</title>
</head><body>
<h1>OK</h1>
<p>The server is temporarily unable to service your
request due to maintenance downtime or capacity
problems. Please try again later.</p>
</body></html>
3
  • have you tried to change user agent?
    – pomaxa
    Sep 28, 2012 at 10:42
  • the server ignores user agents, the data IS downloaded, but before it's downloaded, my browser downloads the html response and thinks everything is done
    – Gabriel
    Sep 28, 2012 at 10:43
  • Maybe you could fiddle with the Accept HTTP header and see if that changes anything.
    – Ja͢ck
    Oct 1, 2012 at 2:25

2 Answers 2

1

Well this is what the cURL optin CURLOPT_RETURNTRANSFER you are using actually is for: to return the transferred content to the user agent.

2
  • but if I understand correctly, when CURLOPT_RETURNTRANSFER is set to false curl will print out the received data. If set to true it will return the data as string. The problem is that i don't want to print out the data, just save it to a file
    – Gabriel
    Sep 28, 2012 at 10:57
  • You are right, sorry, my mistake. I do remember fiddling with the same issue some 4 years ago. I got it to work, I just cannot remember what the cause was currently. Sorry!
    – arkascha
    Sep 28, 2012 at 11:05
0

Your request was OK , but server returned the content as html page (may be their API is not working) so you are getting the maintenance page as reply. looking at php.net

  CURLOPT_RETURNTRANSFER    TRUE 
to return the transfer as a string of the return value of curl_exec()
 instead of outputting it out directly. 

when i used curl to

 <?php
$ch = curl_init("http://localhost/testjson.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$data = curl_exec ($ch);
print_r($data);
?>

what i got after exec is

{"result":["google","stack","facebook"]}

(this is because i printed the result using print_r($data);)

testjson.php is

<?php 
$data   = array ('result'=>array('google','stack','facebook'));
header('content-type:application/json');
echo json_encode($data);

?>
1
  • looking at the response type it's set to application\json ... :/
    – Gabriel
    Sep 28, 2012 at 11:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.