问题
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 request failed! in C:\Projects\app\file.php on line 26
failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request in C:\Projects\app\file.php on line 26
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 );
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.
回答1:
Change mb_strlen(serialize($dataraw), '8bit') to strlen($data)
来源:https://stackoverflow.com/questions/29014016/bad-request-using-file-get-contents-for-put-request-in-php