繁体   English   中英

PHP cURL JSON API

[英]PHP cURL JSON API

我的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。

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

我把

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

代替

$str_data = json_encode($data);

而且似乎现在正在工作。

谢谢

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM