PHP CURL后台进程 - REST API URL

时间:2016-03-03 07:25:56

标签: php rest curl

我正在尝试使用Curl执行后台REST API调用 php中的库。我猜它不起作用。

你能建议我吗?

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

更新:剔除错误说"已达到超时"。

谢谢, Raja K

1 个答案:

答案 0 :(得分: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。只是给你举个例子。