发布请求,file_get_content不起作用

时间:2017-01-11 08:57:09

标签: php post token file-get-contents

我正在尝试使用file_get_contents“发布”到网址并获取身份验证令牌。我遇到的问题是它返回一个错误。

Message: file_get_contents(something): failed to open stream: HTTP request failed! HTTP/1.1 411 Length Required.

我不确定是什么造成了这个但需要帮助。这是功能。

public function ForToken(){
        $username = 'gettoken';
        $password = 'something';
        $url = 'https://something.com';
        $context = stream_context_create(array (
            'http' => array (
            'header' => 'Authorization: Basic ' . base64_encode("$username:$password"),
             'method' => 'POST'
            )
        ));

        $token = file_get_contents($url, false, $context);
        if(token){
            var_dump($token);
        }else{
            return $resultArray['result'] = 'getting token failed';
        }
    }

我用POSTMAN尝试过它,它有效,所以我遇到的唯一问题就是为什么它不适用于file_get_contents。

4 个答案:

答案 0 :(得分:2)

无法命令所以我会这样做。如果您使用Postman,为什么不让Postman为您生成代码。在邮递员中,您可以根据请求单击代码,甚至可以选择您想要的编码语言。像PHP一样使用cURL:

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "http://somthing.com/",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_HTTPHEADER => array(
    "password: somthing",
    "username: gettoken"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}

希望这会有所帮助!

答案 1 :(得分:0)

由于我的声誉,我无法发表评论;

在某些服务器中,file_get_content不可用...

您可以尝试使用CURL,例如this

答案 2 :(得分:0)

您的请求没有任何数据和内容长度丢失。

你可以试试这个:

public function ForToken(){
    $username = 'gettoken';
    $password = 'something';
    $url = 'https://something.com';

    $data = array('foo' => 'some data');
    $data = http_build_query($data);

    $context_options = array (
    'http' => array (
        'method' => 'POST',
        'header'=> "Content-type: application/x-www-form-urlencoded\r\n"
            . "Authorization: Basic " . base64_encode("$username:$password") . "\r\n"
            . "Content-Length: " . strlen($data) . "\r\n",
        'content' => $data
        )
    );

    $context = context_create_stream($context_options);

    $token = file_get_contents($url, false, $context);
    if(token){
        var_dump($token);
    }else{
        return $resultArray['result'] = 'getting token failed';
    }
}

这是类似的问题:

How to fix 411 Length Required error with file_get_contents and the expedia XML API?

答案 3 :(得分:0)

RFC2616 10.4.12     http://tools.ietf.org/html/rfc2616#section-10.4.12

代码:(不起作用,见下文)

public function ForToken(){
$username = 'gettoken';
$password = 'something';
$url = 'https://something.com';
$context = stream_context_create(array (
'http' => array (
'header' => 'Authorization: Basic ' . base64_encode("$username:$password") . 'Content-Length: ' . strlen($url) . '\r\n',
'method' => 'POST'
)
));
$token = file_get_contents($url, false, $context);
if(token){
var_dump($token);
}else{
return $resultArray['result'] = 'getting token failed';
}
}

第二次尝试:

public function ForToken(){
$username = 'gettoken';
$password = 'something';
$url = 'https://something.com';
$context_options = array (
'http' => array (
'method' => 'POST',
'header'=> "Content-type: application/x-www-form-urlencoded\r\n" . "Authorization: Basic " . base64_encode("$username:$password") . "Content-Length: " . strlen($url) . "\r\n",
'content' => $url
)
);
$context = stream_context_create($context_options);
$token = file_get_contents($url, false, $context);
if(token){
var_dump($token);
}else{
return $resultArray['result'] = 'getting token failed';
}
}

这是来自patryk-uszynski用OP代码格式化的答案。