在PHP中使用file_get_contents进行PUT请求的错误请求

时间:2015-03-12 15:35:34

标签: php rest google-app-engine put

这个api调用使用Postman(一个REST客户端)工作正常, 但是当我在GAE应用程序中的服务器上发出请求时,我当前收到以下错误:

  

HTTP请求失败!在C:\ Projects \ app \ file.php第26行

     

无法打开流:HTTP请求失败!第26行的C:\ Projects \ app \ file.php中的HTTP / 1.0 400错误请求

以下是我的应用程序中的代码,该代码调用其余的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。

1 个答案:

答案 0 :(得分:1)

将mb_strlen(序列化($ dataraw),' 8bit')更改为strlen($ data)