PHP cURL JSON API [英] PHP cURL JSON API
本文介绍了PHP cURL JSON API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的API文档说我需要输入以下cURL.
My API documentation says I need to enter the following 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发布数据放入.
I've no idea what to do with the -F bits (nor what -F stands for!) So I put them in as JSON post data.
我假设-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.
But all I get is a page that says error occurred. It should return JSON with login data.
推荐答案
正如Marc所说,我通过json而不是普通字段数据进行发送.
As Marc said, I was sending through json instead of normal field data.
我放入
foreach($data as $key=>$value) { $str_data .= $key.'='.$value.'&'; }
rtrim($str_data, '&');
代替
$str_data = json_encode($data);
它现在似乎正在工作.
谢谢
这篇关于PHP cURL JSON API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文