1

I have this php code, and it works, but the contents of request.json are being posted as a string encapsulating the file contents.

I want it to be posted the file contents directly (a text file containing json). How do I need to change this code so that $payload not being re-encapsulated as a string before sending?

I am guessing my 'content' => json_encode( $payload ) line needs to change, but do not know PHP enough to know how to change it.

$url = 'http://api.phantomjscloud.com/examples/helpers/requestdata';
$payload = file_get_contents ( 'request.json' );

$options = array(
    'http' => array(
        'header'  => "Content-type: application/json\r\n",
        'method'  => 'POST',
        'content' => json_encode( $payload )
    )
);
$context  = stream_context_create($options);
$result = file_get_contents($url, false, $context);
if ($result === FALSE) { /* Handle error */ }

var_dump($result);

PS: I don't want to use 3rd party libraries like curl or zend. Just Php!

EDIT 1: clarification for those complaining json is just text.

EDIT 2:

when trying either 'content' => json_decode( $payload )

I get the error:

Warning: file_get_contents(http://api.phantomjscloud.com/examples/helpers/requestdata): failed to open stream: HTTP request failed! HTTP/1.0 411 Length Required
 in request.php on line 15
bool(false)

my request.json contents are simple, if that matters:

{
    "hi":"world"
}

EDIT 3:

@Nasreddine answer is right. my request.json file wasn't actually correct json (i didn't encapsulate the keys in double-quotes) so I was getting a http-response error for bad json posted.

thank you, my bad :(

JasonS
  • 7,443
  • 5
  • 41
  • 61
  • 3
    Your question doesn't make any sense. [There is no such thing as a JSON object](http://benalman.com/news/2010/03/theres-no-such-thing-as-a-json/). JSON is a text based data format. HTTP is a protocol for transferring text. Converting an object to a string of JSON and then including it in the request body is how you make a JSON request with HTTP. – Quentin May 22 '16 at 18:59
  • sorry for the confusion, the problem is that the string from request.json is being escaped as json again, so it's sending a string of the file contents, not the file contents directly – JasonS May 22 '16 at 19:00
  • You need to send a `Content-Length` header according to the HTTP response code. – Charlotte Dunois May 22 '16 at 19:11
  • http://stackoverflow.com/questions/9412650/how-to-fix-411-length-required-error-with-file-get-contents-and-the-expedia-xml – Mike May 22 '16 at 19:12

2 Answers2

4

You're re-encoding what is already json as a json text. So instead of this:

'content' => json_encode( $payload )

Use this:

'content' => $payload
Nasreddine
  • 36,610
  • 17
  • 75
  • 94
1

Try this:

'content' => $payload

Eventually this:

'content' => json_decode( $payload )

nospor
  • 4,190
  • 1
  • 16
  • 25
  • this doesn't work unfortuantely. I think it would if my file was encapsulated in a string though. The solution is Nasreddine's. thank you – JasonS May 22 '16 at 19:09
  • Well.... I also said what Nasreddine `'content' => $payload`. I do not understand why you can not see that in my answer.... Never mind... – nospor May 22 '16 at 19:11