Content-length in php file_get_content post request

4.1k Views Asked by At

I am trying to update Google fussion table through a php post request. URL works perfectly fine on Auth PlayGround. See the image below. Sorry! Stack overflow is not allowing me to post image, please use this link Image

But when I try the same using php post request file_get_content, it shoots with an error "411 Length Required". What length is the code asking for. Obviously its not content length because its "Zero" in this case.

<?php
$url = https://www.googleapis.com/fusiontables/v1/query?sql=INSERT INTO 1f_Z_********bQ-br8g17rFWBknri03fz-EQc (Name, Phone) VALUES ('Anees Hameed', '9895435751')
$Post = array('http' =>
    array(
        'method'  => 'POST',
        'header'  => 'Content-type: application/json',
        'header'  => 'GData Version: 3.0',
        'header'  => 'Authorization: Bearer '.$_Session[access_token]
    )
);
$Post= stream_context_create($Post);
$request = file_get_content($url, false, $Post);
?>

How to get rid of this error.

1

There are 1 best solutions below

5
On

First of all what's that URL in your code?! and you didn't wrapped it within quotes either.

411 error

This error seldom occurs in most Web traffic, particularly when the client system is a Web browser. The problem can only be resolved by examining what your client system is trying to do then discussing with your ISP why the Web server expects a 'Content-Length' specification.

and your code:

<?php
$url = "https://www.googleapis.com/";
$content = "this is something to be sent to the server";
$Post = array('http' =>
    array(
        'method'  => 'POST',
        'header'  => 'Content-type: application/json',
        'header'  => 'GData Version: 3.0',
        'header'  => 'Authorization: Bearer '.$_Session[access_token],
        'header'  => 'Content-Length: '.strlen($content)
    )
);
$Post= stream_context_create($Post);
$request = file_get_content($url, false, $Post);
?>