2011-10-09 61 views
0

我有一个前端代码卷曲操作和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"} 

回答

0

你曾经通过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); 
+0

我试过了,但没有运气。 – lilzz

+0

你可以发布你的服务器的Header响应,对于那个特定的文件,确保它不压缩它或需要HTTP认证! – adrian7

0

尝试的var_dump($ responseJSON)

如果返回false尝试

curl_error($ ch)

返回最后一个cURL操作的明文错误消息。

您确定自己的$ url无误吗?

+0

$ url是正确的,否则后端代码不会拦截它并处理。 var_dump($ responseJSON)返回字符串(1153)。 curl_error($ ch)不返回任何内容。 – lilzz

+0

随着新的信息,我认为你有你的API调试消息启用的地方,如果你关掉它/删除它,你应该得到你的回应 – Quurks