卷曲操作和PHP
问题描述:
我有一个前端代码卷曲操作和PHP
$ch = curl_init();
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
//curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json'));
curl_setopt($ch, CURLOPT_URL, $url);
//make the request
$responseJSON = curl_exec($ch);
$response_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($response_status == 200) { // success
// remove any "problematic" characters from the json string and then decode
if (debug) {
echo "----finish API of getAPI inside basic_function with status==200---";
echo "<br>";
echo "-------the json response is-------" ; //.$responseJSON;
var_dump($responseJSON);
//print_r($responseJSON);
echo "<br>";
}
return json_decode(preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $responseJSON));
}
,我有卷曲时,解雇其URL的操作,其执行的后端代码。因此后端代码将被激活。所以,我知道cURL正在运行。
$output=array (
'status'=>'OK',
'data'=>'12345'
)
$output=json_encode($output)
echo $output;
和$output
显示在浏览器{"status":"OK","data":"12345"}
然而,我又回到了前端代码和做echo $responseJSON
,我什么也没得到。我认为{"status":"OK","data":"12345"}
的输出会去$responseJSON
。任何想法?
这是浏览器的输出,有些东西很奇怪! response_status得到了200,即使在后端代码解析API之前,它也是成功的。我期待status = 200和json响应后的{“status”:“OK”,“data”:“12345”}
=================== ================================================== ====================
的基本功能
-------url of cURL is -----http://localhost/test/api/session/login/?device_duid=website&UserName=joe&Password=1234&Submit=Submit
----finish API of getAPI inside basic_function with status==200---
-------the json response is-------string(1153)
"************inside Backend API.php******************
---command of api is--/session/login/
---first element of api is--UserName=joe
--second element of api is---Password=1234
---third element of api is----Submit=Submit
----fourth element of api is---
-------inside session login of api-------------
{"status":"OK","data":"12345"}
答
你曾经通过curl_setopt($ch, CURLOPT_TIMEOUT, 10);
尝试获取API的内部评价? 如果您评论该行,请查看发生了什么事情。
也可以尝试用一个基本的代码,如果该作品,smthing你后来添加的是错误的:
// create a new cURL resource
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/");
curl_setopt($ch, CURLOPT_HEADER, false);
// grab URL and pass it to the browser
curl_exec($ch);
// close cURL resource, and free up system resources
curl_close($ch);
我试过了,但没有运气。 – lilzz
你可以发布你的服务器的Header响应,对于那个特定的文件,确保它不压缩它或需要HTTP认证! – adrian7