2017-01-11 23 views
4

我想使用file_get_contents“发布”到一个url并获得auth令牌。我遇到的问题是它返回一个错误。发送请求file_get_content not working

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一起工作。

+0

一些东西必须与你的'$ context'混淆,并且'Content-Length'标题丢失。 –

+0

@ patryk-uszynski如何添加Content-Length? –

+0

'file_get_content'有一些限制。试用CURL –

回答

0

我不能评论,因为我的名声;

在一些服务器file_get_content不可用......

您可卷曲尝试像this

+0

接受了您的建议,用CURL试用了它,但始终返回false .. –

+0

您能否将CURL代码发布到主帖以备将来参考? – ahmetertem

+0

创建了一个粘贴bin,以便您可以检查 http://pastebin.com/YdWd8hu3 –

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?

+0

用户名和密码在哪里? –

+0

@Masnad Nihit和以前一样。您必须将它们连接到标题字符串中,并用'\ r \ n'分开 –

+0

我正在尝试您的示例但未通过,请您更新您的示例但添加我的信息。 –

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的代码格式化。

+0

这段代码不工作,不幸的是。 –

+0

真的吗?;(。我想我有它....我会再试一次。 –

+0

我的双引号我认为是问题 –

2

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

$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; 
} 

希望这会有所帮助!

+0

收到此错误 卷曲错误#:SSL证书的问题:无法获得放入这行代码在curl_setop_array CURLOPT_SSL_VERIFYPEER本地颁发者证书 –

+0

=>假, –

+0

一个新的错误,现在 HTTP错误411的请求必须进行分块或者有一个内容长度。 –