PHP cURL JSON API

TempPeck

我的API文档说我需要输入以下cURL。

卷曲

 -H "Accept: application/json"
 -F grant_type=consumer_credentials 
 -F consumer_key=ABCDEFGHIJKLMN
 -F consumer_secret=123456789
 https://sandbox.apps.****.com/api/AccessToken 

我不知道该如何处理-F位(也不知道-F代表什么!),所以我将它们作为JSON发布数据放入。

我假设-H是标题?

作为起点,我有以下几点:

$data = array(
  "grant_type" => "consumer_credentials",
  "consumer_key" => "ABCDEFGHIJKLMN",
  "consumer_secret" => "123456789"
);

$str_data = json_encode($data);

$url_send ="https://sandbox.apps.****.com/api/AccessToken";

function sendPostData($url, $post){
  $ch = curl_init($url);
  curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");  
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_POSTFIELDS,$post);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
  curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json'));  
  $result = curl_exec($ch);
  curl_close($ch); 
  return $result;
}

echo " " . sendPostData($url_send, $str_data);

但是我所得到的只是一个页面,显示发生了错误。它应该返回带有登录数据的JSON。

TempPeck

正如Marc所说,我是通过json而不是普通字段数据发送的。

我把

foreach($data as $key=>$value) { $str_data .= $key.'='.$value.'&'; }
rtrim($str_data, '&');

代替

$str_data = json_encode($data);

而且似乎现在正在工作。

谢谢

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章