在PHP中使用file_get_contents进行PUT请求的错误请求 [英] Bad request using file_get_contents for PUT request in PHP
问题描述
此API调用可以使用Postman(REST客户端)正常运行, 但是在我的GAE应用程序中的服务器上发出请求时,当前出现以下错误:
This api call works fine using Postman (a REST Client), but when making the request on the server in my GAE application I am currently getting the following error:
HTTP请求失败!在第26行的C:\ Projects \ app \ file.php中
HTTP request failed! in C:\Projects\app\file.php on line 26
无法打开流:HTTP请求失败!第26行的C:\ Projects \ app \ file.php中的HTTP/1.0 400错误请求
这是我的应用程序中的代码,该代码调用其余的api方法/exampleMethod
Here is the code from my application, which makes a call to the rest api method /exampleMethod
$dataraw = [
'email' => 'email@ex.com',
'first_name' => 'firstname',
'last_name' => 'lastname',
'fEmail' => 'email@ex.com',
'message' => 'test'
];
$data = http_build_query ( $dataraw );
$context = [
'http' => [
'method' => 'PUT',
'header' => "Authorization: apikeystring\r\n" . "Content-Length: " . strlen($data) . "\r\n" . "Content-Type: application/json\r\n",
'content' => $data,
'proxy' => 'tcp://euproxy.example.com:0000'
]
];
$context = stream_context_create ( $context );
$result = file_get_contents ( $GLOBALS ['REST_API_URL'] . '/exampleMethod', false, $context );
在该应用程序中,我已经成功地将file_get_contents用于GET请求,但是通过此PUT请求,我收到了一个400错误的请求,该请求不是来自Rest Api.
In the application I have successfully used file_get_contents for GET requests, but with this PUT request I am getting a 400 bad request which is not coming from the Rest Api.
推荐答案
将mb_strlen(serialize($ dataraw),'8bit')更改为strlen($ data)
Change mb_strlen(serialize($dataraw), '8bit') to strlen($data)
这篇关于在PHP中使用file_get_contents进行PUT请求的错误请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!