简体   繁体   中英

PHP script with curl returns 200 response

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>

嗯,这就是您实际使用的cURL optin CURLOPT_RETURNTRANSFER的作用:将传输的内容返回给用户代理。

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

?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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