卷曲的麻烦

时间:2015-11-12 19:51:53

标签: php curl

您好我正在尝试使用pushbots api来获取我应用的分析结果。问题是我不太了解curl或如何实现它。

我希望有人可以解释我如何使用下面粘贴的代码。

curl -X GET \
-H "x-pushbots-appid: xxxxxxxxxxxxxxxxxxxxxxx" \
-H "x-pushbots-secret: xxxxxxxxxxxxxxxxxxxxx" \
-H "Content-Type: application/json" \
https://api.pushbots.com/analytics

修改 我现在正在使用代码

<?php
$headers = array("Content-Type: application/json","x-pushbots-appid: XXXXXXXXXXXXXX","x-pushbots-secret: XXXXXXXXXXXXXXXXXXXXXX");

$appid = "x-pushbots-appid: XXXXXXXXXXXXXXXXXXXXXXXXX";
$secretid = "x-pushbots-secret: XXXXXXXXXXXXXXXXXXXXXXX";

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL,"api.pushbots.com/analytics");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $appid&$secretid);


curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//set the headers now
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$output = curl_exec($ch);

curl_close ($ch);

var_dump($output);


?>

我现在收到此错误:

string(64) "{"code":"MethodNotAllowedError","message":"POST is not allowed"}" 

1 个答案:

答案 0 :(得分:1)

1)。使用GET发送cURL个请求: - https://api.pushbots.com/analytics

2)。您需要发送的数据字段为x-pushbots-appidx-pushbots-secret

3)。标头应为Content-Type: application/json

这是一个简单的例子。

<?php
$headers = "Content-Type: application/json";
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL,"https://api.pushbots.com/analytics");
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET"); 
curl_setopt($ch, CURLOPT_POSTFIELDS,
            "x-pushbots-appid=somevalue&x-pushbots-secret=somevalue");


curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//set the headers now
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$output = curl_exec($ch);

curl_close ($ch);

var_dump($output);
?>