使用PHP中的CURL进行推送调用到api

时间:2013-07-08 13:47:56

标签: php html ajax api curl

我是API调用的新手

我需要在PHP中执行以下CURL调用:链接到API文档http://dev.ducksboard.com/apidoc/tutorial/#overview

curl -v -u APIKEY:x -d '{"value": 10}' https://push.ducksboard.com/values/235

我有以下更新的代码(但不能正常工作):

 <?php


$postData = array('value' => 10);


$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://push.ducksboard.com/v/*****');
curl_setopt($ch, CURLOPT_USERPWD, "000000000000000");
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postData));
 curl_setopt($ch, CURLOPT_POST, true); 

  $result=curl_exec ($ch);
  curl_close ($ch);
  echo $result;
?>

2 个答案:

答案 0 :(得分:1)

您需要传递值CURLOPT_POSTFIELDS

$postData = array('value' => 10);
curl_setopt($ch, CURLOPT_USERPWD, "APIKEY:0000000");
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postData)); 

还将CURLOPT_POST设置为true

curl_setopt($ch, CURLOPT_POST, true); 

答案 1 :(得分:1)

api想要一个将被忽略的密码,下面是工作代码

<?php


// encode the json object
$postData = json_encode(array('value' => 22));

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://push.ducksboard.com/v/address');
curl_setopt($ch, CURLOPT_USERPWD, "0000000000000000000000:IGNORED");
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_POST, true);

$result=curl_exec ($ch);
curl_close ($ch);
echo $result;
?>