4

I am trying to use file_get_contents to "post" to a url and get auth token. The problem I am having is that it returns a error.

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

I am not sure what is causing this but need help. Here is the function.

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

I tried it with POSTMAN, and it works, so only problem I am having is that why isnt it working with file_get_contents.

Masnad Nihit
  • 1,986
  • 2
  • 21
  • 40

4 Answers4

3

Cannot command so i will do it this way. If you use Postman why don't you let Postman generate the code for you. In postman you can click code at a request and you can even select in what coding language you wanne have it. Like PHP with 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;
}

Hope this will help!

Wouter Veen
  • 120
  • 9
0

I can not comment because of my reputation;

In some servers file_get_content is not available...

You may try with CURL like this

Community
  • 1
  • 1
ahmetertem
  • 242
  • 6
  • 14
0

Your request havent any data and content length is missing.

You can try this:

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

Here is similar problem:

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

Community
  • 1
  • 1
Patryk Uszyński
  • 1,069
  • 1
  • 12
  • 20
0

RFC2616 10.4.12 https://www.rfc-editor.org/rfc/rfc2616#section-10.4.12

Code: (Doesn't work, see below)

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

2nd Try:

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

This is from patryk-uszynski's answer formatted with the OP's code.

Community
  • 1
  • 1
Trae Abell
  • 540
  • 4
  • 7