HTTP 请求失败!使用文件获取内容

HTTP request failed! with file get contents

我使用 Dropbox 的 OAuth 来获取访问令牌:https://blogs.dropbox.com/developers/2012/07/using-oauth-1-0-with-the-plaintext-signature-method/

但我收到错误信息: 警告:file_get_contents(https://api.dropbox.com/1/oauth/request_token):无法打开流:HTTP 请求失败! HTTP/1.1 400 错误请求

我的PHP代码:

$Header = json_encode(array('Authorization: OAuth oauth_version="1.0"', "oauth_signature_method" => "PLAINTEXT", "oauth_consumer_key" => "XX", "oauth_signature" => "XX"));

$Options = array('http' =>
    array(
        'method'  => 'POST',
        'header'  => $Header,
    )
);


$Context  = stream_context_create($Options);
$Result = file_get_contents("https://api.dropbox.com/1/oauth/request_token", false, $Context);

print_r($Result);

根据 http://php.net/manual/en/context.http.php 上的文档,header 选项似乎只是一个普通字符串,而不是 json_encoded。或者,您应该能够使用 headers 的数字索引数组,如下所示:

$Header = array(
"Authorization: OAuth oauth_version=\"1.0\", oauth_signature_method=\"PLAINTEXT\", oauth_consumer_key=\"XXXXX\", oauth_signature=\"XXXXX&\"\r\n"
);

$Options = array('http' =>
    array(
        'method'  => 'POST',
        'header'  => $Header,
    )
);

这是错误的:

$Header = json_encode(array('Authorization: OAuth oauth_version="1.0"', "oauth_signature_method" => "PLAINTEXT", "oauth_consumer_key" => "XX", "oauth_signature" => "XX"));

http 流中的 header 参数可以是包含一个 header key: value 的简单字符串,也可以是 key: value 字符串数组。您将 json 字符串作为 header 填充,这意味着它不是有效的 http header。

完全删除 json_encode() 部分。 $Header = array(...) 是你所需要的。