2011-02-10 80 views
1

我想要使用PHP和Curl將表單數據發佈到遠程服務器,我也希望獲得服務器對發佈數據的響應。PHP + CURL示例來發布表單數據並接收響應

我已經寫了一個函數使用Curl將數據發佈到服務器。不過,我想獲得對發佈數據的服務器響應。服務器響應將是JSON

所以我期待寫這樣的功能:

public function postFormDataAndGetResponse() 
{ 
    // use curl to post form data (this I have already done) 

    // now what Curl options to use to wait (up to a TIMEOUT interval) to receive json data from server? 
} 

我會任何幫助

+0

回滾,因爲它可能對其他人有用。 (順便說一句,IIRC你可以在15分鐘後接受) – Piskvor 2011-02-10 16:29:24

回答

3

在這裏你去,簡單的例子感激:

 
$ch = curl_init($url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
$recived_content = curl_exec($ch); 
$data = json_decode($recived_content); 
+0

如果你需要,使用curl_getinfo($ ch,CURLINFO_HTTP_CODE)來獲得http響應代碼。 – MatejB 2011-02-10 16:26:02