使用PHP从api获取数据
我的任务是使用PHP从api获取数据。 它使用POST请求,该请求还发送json正文。 没有提到设置任何标题,这让我很困惑。我以前用过ajax处理api,从来没有用过php。由于json是双向的,既可以发送到api,也可以作为api的响应,所以我有点困惑 我试着用php的方式来做,但在很多帖子上都说这是不可取的。 很多人都说使用cURL是正确的方法,所以我猜,在不涉及第三方库的情况下,这是正确的方法,在这种情况下,我(也)不能使用第三方库 到目前为止,我的代码是: Json正文:使用PHP从api获取数据,php,json,curl,http-post,Php,Json,Curl,Http Post,我的任务是使用PHP从api获取数据。 它使用POST请求,该请求还发送json正文。 没有提到设置任何标题,这让我很困惑。我以前用过ajax处理api,从来没有用过php。由于json是双向的,既可以发送到api,也可以作为api的响应,所以我有点困惑 我试着用php的方式来做,但在很多帖子上都说这是不可取的。 很多人都说使用cURL是正确的方法,所以我猜,在不涉及第三方库的情况下,这是正确的方法,在这种情况下,我(也)不能使用第三方库 到目前为止,我的代码是: Json正文: $str =
$str = '{"username":"mymail@mymail.com","password":"blah"}';
//or
$arr = [
"username" => "mymail@mymail.com",
"password" => "blah"
];
json和非json示例都会产生相同的错误:
{"type":"https://tools.ietf.org/html/rfc7231#section-6.5.13","title":"Unsupported Media Type","status":415,"traceId":"80002328-0002-ff00-b63f-84710c7967bb"}
如果我使用alternative header作为示例2,它将生成不同的头:
{"errors":{"":["Input string '--------------------------7abe186689ac2978' is not a valid number. Path '', line 1, position 42."]},"title":"One or more validation errors occurred.","status":400,"traceId":"8000232a-0002-ff00-b63f-84710c7967bb"}
在我的api文档中,并没有提到设置任何头,尽管如此,我甚至尝试设置头。两种方法都不起作用。只是抛出一个错误。所以要么我在cURL中遗漏了什么,要么他们给我发送了不完整的文档。不管怎样,我都不确定,因为我从未使用过cURL,也从未使用过这个api
我在《邮递员》中也试过,有标题也有,没有,我甚至试过用引号设置json正文(我知道这很愚蠢),只是为了排除任何可能性。
这些都不管用
任何一个好的人都可以根据之前所说的或者至少为我指明了正确的方向来为我提供解决方案
Edit1:
尝试了Brian的头部方法,剩下的代码是我的
function post2($url,$data){
$headers = array(
"Accept-Encoding: gzip",
"Content-Type: application/json"
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
//$query = $data;
$query = http_build_query($data);
curl_setopt($ch,CURLOPT_POST, true);
curl_setopt($ch,CURLOPT_POSTFIELDS, $query);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
我试过它的4种变体。
1.数据作为数组和$query=$Data代码>。
错误:
数据作为字符串和$query=$Data代码>。
错误:
数据作为数组和$query=http\u build\u query($Data)代码>。
错误:
数据为字符串和$query=http\u build\u query($Data)代码>。
错误:
Edit2:
如果我使用带有json头的Postman
:
错误:
如果我使用不带json头的Postman
:
错误:
Edit3:
好的,我用我自己制作的经过验证的api尝试了我的原始代码。无论是json还是查询参数示例,原始代码的两种变体都很好。我的api使用标准ajax(jquery和vanilla js)、Postman进行测试,现在甚至还使用PHP中的cURL进行测试。其中的Readme.md非常冗长,而且其中的要点很容易测试 更新
好的,现在我们把标题拿出来,因为它没有帮助
我在httpheader中添加了Expect:in,因为根据curl_setopt上的注释,一些web服务器在使用post时需要它
您是否尝试在数组中发送查询字符串?(见下文)
帕特森试过了。仍然是错误。想看看我编辑的帖子吗?也许我遗漏了什么。正如我所提到的,我也试着用Postman',发送json(用户名和密码),但即使是Postman'也会抛出一个错误。是否有可能我得到了错误的api文档?仍然是相同的错误,{“type”:"https://tools.ietf.org/htm ..
。好的,我添加了json解码,你能在我的回答中尝试更新的函数吗?这不起作用。http\u build\u query
接受$arr
已经存在的关联数组。传递到“json\u decode”没有任何作用。如果你将json字符串传递到“json\u decode”,然后再传递它http\u build\u query
,与没有“json\u decode”的post3($url,$arr)
相同。
function post2($url,$data){
$headers = array(
"Accept-Encoding: gzip",
"Content-Type: application/json"
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
//$query = $data;
$query = http_build_query($data);
curl_setopt($ch,CURLOPT_POST, true);
curl_setopt($ch,CURLOPT_POSTFIELDS, $query);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
{"errors":{"":["Input string '--------------------------ae9f656894b99247' is not a valid number. Path '', line 1, position 42."]},"title":"One or more validation errors occurred.","status":400,"traceId":"80002dd0-0002-fe00-b63f-84710c7967bb"}
Internal server error
{"errors":{"":["Unexpected character encountered while parsing value: k. Path '', line 0, position 0."]},"title":"One or more validation errors occurred.","status":400,"traceId":"80002dd2-0002-fe00-b63f-84710c7967bb"}
Warning: http_build_query(): Parameter 1 expected to be Array or Object. Incorrect value given in C:\xampp\htdocs\xmlovi\test1.php on line 56
{"errors":{"":["A non-empty request body is required."]},"title":"One or more validation errors occurred.","status":400,"traceId":"800022db-0002-f700-b63f-84710c7967bb"}
Internal server error
{
"type": "https://tools.ietf.org/html/rfc7231#section-6.5.13",
"title": "Unsupported Media Type",
"status": 415,
"traceId": "80002dda-0002-fe00-b63f-84710c7967bb"
}
function post1($url,$data){
$ch = curl_init();
//Example1
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER,array("Expect:"));
$query = http_build_query($data, '', '&');
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POSTFIELDS, array($query));
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
//Example1
//echo post1($url1,$str);
//VS
//Example2
echo post1($url1,$arr);