2016-03-03 36 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 –