PHP 中的 PUT 请求使用 file_get_contents 的错误请求
Bad request using file_get_contents for PUT request in PHP
这个 api 调用使用 Postman(一个 REST 客户端)工作正常,
但是在我的 GAE 应用程序中向服务器发出请求时,我目前收到以下错误:
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
这是我的应用程序的代码,它调用了其余 api 方法 /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。
将 mb_strlen(serialize($dataraw), '8bit') 更改为 strlen($data)
这个 api 调用使用 Postman(一个 REST 客户端)工作正常, 但是在我的 GAE 应用程序中向服务器发出请求时,我目前收到以下错误:
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
这是我的应用程序的代码,它调用了其余 api 方法 /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。
将 mb_strlen(serialize($dataraw), '8bit') 更改为 strlen($data)