【问题标题】:Trouble with Curl卷曲问题
【发布时间】:2016-02-14 06:57:15
【问题描述】:

嗨,我正在尝试使用 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"}" 

【问题讨论】:

  • 好的,从我可以理解的问题中,您想要解释您的代码以及如何使用它。 Stackoverflow 不是我想回答这个问题的地方。看看 Code Review,就像它的名字说它是一个代码审查网站一样,也许使用该网站的人可以深入解释它。
  • 嗨@CollinKoornstra 我很感激你试图提供帮助,但这个问题完全是代码审查的题外话。 CR 的黄金法则是代码必须已经按预期工作。解释/修复代码不是我们所做的(它准确地是 SO 的用途)。您可能想阅读A Guide to CR for Stack Overflow Users

标签: php curl


【解决方案1】:

1)。使用cURL 发送GET 请求到:- 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);
?>

【讨论】:

  • 你能给我举个例子吗?
  • @AmandoVledder 我已经更新了我的答案并提供了一个快速解决方案。
  • 这只是一个示例代码。根据您的需要进行修改。
  • 我编辑了我的帖子,现在它给了我一个错误。你能帮我解决这个问题吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-24
  • 2016-02-07
  • 2011-03-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多