2016-03-03 116 views
0

我試圖在php中執行後臺REST API調用Curl 庫。我想這是行不通的。PHP CURL後臺進程 - REST API URL

你能建議我嗎?

$cum_url  = http://localhost/test/list; 
$post = [ 'id' => $object->id ]; 
$ch = curl_init($cum_url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_HEADER, false);   
curl_setopt($ch, CURLOPT_NOBODY, true); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $post); 
curl_setopt($ch, CURLOPT_FRESH_CONNECT, true); 
curl_setopt($ch, CURLOPT_TIMEOUT_MS, 1); 
curl_exec($ch); 
curl_close($ch); 

UPDATE:卡爾錯誤說 「超時達到」。

感謝, 拉賈ķ

+0

參考:http://www.paul-norman.co.uk/2009/06/asynchronous-curl-requests/ – user1764882

+0

請在問題中包含您的錯誤或其他發現。你怎麼知道它不工作?我希望你沒有真正猜到,但先測試一下;) – SubliemeSiem

+0

嗨,謝謝。我在REST URL中添加了一個db插入行。這不是ping。在添加了curl_setopt($ ch,CURLOPT_FRESH_CONNECT,true)之後它不工作。 curl_setopt($ ch,CURLOPT_TIMEOUT_MS,1);爲後臺進程 – user1764882

回答

0

您需要從陣列傳輸您的數據發佈到HTTP參數字符串,例如:

$cum_url = "http://localhost/test/list"; 
$post = [ 'id' => $object->id ]; 
$postdata = http_build_query($post); 

$options = array (CURLOPT_RETURNTRANSFER => true, // return web page 
    CURLOPT_HEADER => false, // don't return headers 
    CURLOPT_FOLLOWLOCATION => true, // follow redirects 
    CURLOPT_AUTOREFERER => true, 
    CURLOPT_USERAGENT => "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20041001 Firefox/0.10.1", 
    CURLOPT_SSL_VERIFYHOST => false, 
    CURLOPT_SSL_VERIFYPEER => false); 
$ch = curl_init($cum_url); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData); 
curl_setopt_array ($ch, $options); 
$res = null; 
if(!curl_errno($ch)) { 
    $res = curl_exec($ch); 
} 
curl_close($ch); 

當然,有些選項是可選取決於你,當然CURLOPT_USERAGENT。只是向你展示一個例子。

+0

謝謝。正常CURL正在工作。我正在談論後臺進程。你是否說你是snippet支持後臺進程? – user1764882

+0

請參閱http://php.net/manual/en/function.curl-setopt.php,以確保您瞭解curl選項,並只使用必要的選項,而不僅僅複製其他示例中的選項......還CURLOPT_RETURNTRANSFER當設置爲true時不返回網頁,但將響應作爲字符串返回,而不是直接輸出。 – DTH

+0

對不起,我誤解了你的問題。如果你正在尋找curl發送異步調用,也許你可以試試這個。 http://stackoverflow.com/questions/26039848/php-asynchronous-curl-with-callback –