Bad request using file_get_contents for PUT request in PHP

旧巷老猫 提交于 2019-12-11 04:18:50

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!