cURL操作和PHP

时间:2011-10-09 19:01:28

标签: php curl

我有一个前端代码

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

我有一个后端代码,当cURL用其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之前也是成功的。我希望在{“状态”:“确定”,“数据”:“12345”}

后,状态= 200和json响应

=============================================== ==========================================

在基本功能的get API中

-------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"}

2 个答案:

答案 0 :(得分:0)

您是否尝试使用curl_setopt($ch, CURLOPT_TIMEOUT, 10);发表评论? 看看如果你评论那条线会发生什么。

同时尝试使用基本代码,如果可以,稍后添加的内容是错误的:

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

答案 1 :(得分:0)

尝试var_dump($ responseJSON)

如果返回false,请尝试

  

curl_error($ ch)

     

返回上一次cURL操作的明文错误消息。

你确定你的$ url是否正确?