PHP 卷曲 JSON API [英] PHP cURL JSON API

查看:30
本文介绍了PHP 卷曲 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 是标题?

I assume -H is header?

作为一个起点,我有这个:

As a starting point I had this:

$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);

它现在似乎可以工作了.

and it seems to be working now.

谢谢

这篇关于PHP 卷曲 JSON API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆