How to Post Data in PHP Using File_Get_Contents

How to post data in PHP using file_get_contents?

Sending an HTTP POST request using file_get_contents is not that hard, actually : as you guessed, you have to use the $context parameter.



There's an example given in the PHP manual, at this page : HTTP context options (quoting) :

$postdata = http_build_query(
array(
'var1' => 'some content',
'var2' => 'doh'
)
);

$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-Type: application/x-www-form-urlencoded',
'content' => $postdata
)
);

$context = stream_context_create($opts);

$result = file_get_contents('http://example.com/submit.php', false, $context);

Basically, you have to create a stream, with the right options (there is a full list on that page), and use it as the third parameter to file_get_contents -- nothing more ;-)



As a sidenote : generally speaking, to send HTTP POST requests, we tend to use curl, which provides a lot of options an all -- but streams are one of the nice things of PHP that nobody knows about... too bad...

Not working : Using file_get_contents to post data

I have updated your code using code from the PHP manual and changes like adding PHP tags to 4.php and fixing typo in $_REQUEST.

Here is the updated code, which seems to be working for me:

<?php

$data = array ('foo' => 'bar', 'bar' => 'baz');
$data = http_build_query($data);

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

$context = stream_context_create($context_options);
$result = file_get_contents('http://localhost/php/4.php', false, $context);
var_dump($result);//prints something :)
?>

4.php

<?php

print_r($_REQUEST);
$postdata = http_build_query($_POST);
print_r($postdata);

Sending post data along with file_get_contents()

http://www.php.net/manual/de/context.http.php#101933:

“watch your case when using methods (POST and GET)...it must be always uppercase. in case of you write it in lower case it wont work.”

Not working Using file_get_contents to POST data

This will not worked. Reason is freeformatter.com is not allowing to give API or POST call. They always return you view-source of html page. This is allow to use from UI but not by API call. I tried with CURL post also. its gives return back view-source of html.

Here is code you can try this works well with different


<?php
$data = array ('htmlString' => '<div class="container"><div class="row"><div class="col-md-8">&encoding=UTF-8', 'indentation' => 'THREE_SPACES');
$data = http_build_query($data);
$context_options = array (
'http' => array (
'method' => 'POST',
'header'=> "Content-type: application/x-www-form-urlencoded\r\n"
. "Content-Length: " . strlen($data['htmlString']) . "\r\n",
'content' => $data
)
);

$context = stream_context_create($context_options);
$result = file_get_contents('https://en99otyaenia.x.pipedream.net', false, $context);
var_dump($result);

Code doesnt have any issue. Issue is with URL.

POST data using file_get_contents

I myself found the way to solve this problem.

$json = file_get_contents('php://input');//to getting json post values we need to use
$obj = json_decode($json,true);
ob_start();
include('PDF_Form.php');
$template_html = ob_get_contents();
ob_end_clean();
echo $template_html;

In the PDF_Form.php file I have used like this...

<html>
<head></head>
<input type="text" name="eg" value="
<?php echo $obj['status'];?>
">

</html>

It works perfectly....

Regards,

Rekha

php://input and file_get_contents just output the POST data rather than process it

A standard configuration of a web server is to execute PHP directives only in files with a .php file extension.

You could configure your web server to execute PHP in files with a .jpg file extension (the specifics depend on which web server you are using) but this would be highly unusual — doubly so because a JPEG image is a binary file and not a text file to start with.


Also note that allowing arbitrary PHP to be accepted as user input and then executed on your server is highly dangerous.



I'm aware I won't get executed when viewing the file but shouldn't the PHP I sent in the body get interpreted?

No. Reading a file into a variable only reads a file into a variable. file_get_contents does not execute PHP directives in user input.

That would also be highly dangerous and PHP isn't that bad.

Can't send data in post request via file_get_contents

http_build_query builds application/x-www-form-urlencoded content. (not application/json)

There is a full example:

How to post data in PHP using file_get_contents?



Related Topics



Leave a reply



Submit