2011-10-09 114 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