$opts = array('http' => array('method' => $_SERVER['REQUEST_METHOD'],
'header' => array("Accept-language: en\r\n",
"Cookie: " . session_name() . "=" . session_id() . "\r\n",
" Content-type: application/x-www-form-urlencoded\r\n"),
'content' => $_POST));
$postdata = http_build_query($_POST);
$context = stream_context_create($opts);
session_write_close(); // This is the key
echo $obsah = file_get_contents("http://localhost/journal/", false, $context);
This code is not working with POST and cookies.
The two existing answers are incorrect.
Newlines (\n) do not need to be added to HTTP headers used in stream_context_create(), and furthermore Carriage Returns (\r) should never be used anywhere near HTTP.
The following code is copied from the OP and corrected:
$postdata = http_build_query($_POST, '', '&', PHP_QUERY_RFC3986);
$opts = array(
'http' => array(
'method' => $_SERVER['REQUEST_METHOD'],
'header' => array(
"Accept-language: en",
"Cookie: " . session_name() . "=" . session_id(),
"Content-type: application/x-www-form-urlencoded",
),
'content' => $postdata,
)
);
$context = stream_context_create($opts);
$obsah = file_get_contents("http://localhost/journal/", false, $context);
echo $obsah
I've also added commas to the last array items so that maintenance will not have unnecessary lines in Git / SVN commits, and configured http_build_query() to use the more modern RFC 3986 encoding.
When passing a header with an array() instead of a string, you don't need the \r\n because PHP's stream_context_create() will do it for you on the header.
You also don't need session_write_close().
When you pass multiple headers there should two \r\n before sending POST data.
Let me explain.
Try this:
$opts = array(
'http' => array(
'method' => $_SERVER['REQUEST_METHOD'],
'header' => array(
"Accept-language: en\r\n",
"Cookie: ".session_name()."=".session_id()."\r\n",
"Content-type: application/x-www-form-urlencoded\r\n\r\n"
),
'content' => $_POST
)
);
$postdata = http_build_query($_POST);
$context = stream_context_create($opts);
session_write_close(); // this is the key
echo $obsah = file_get_contents("http://localhost/journal/", false, $context);
Related
Please someone to help me
I'm trying to POST a request with file_get_contents and I receive a 415 code error message. I'm not a pro dev and I want to know what is wrong in my source code. Here is my problem:
I generate a token which is a parameter of the second request
the generated token must be used for the POST request
I receive a 415 Error code message. I don't know how to correct that source
$tok=file_get_contents('https://restapi.bulksmsonline.com/rest/api/v1/sms/gettoken/username/xxxx/password/xxxx');
$toke =json_decode($tok, true);
$token=$toke['token'];
$data = json_encode(
array(
'from' => 'TEST',
'to' => '335546821546',
'type'=> 'Text',
'content'=> 'Test',
'sendDateTime'=> '2020/07/07'
)
);
$options = array('http' =>
array(
'method' => 'POST',
'header' => "Authorization Token " . base64_encode("$token"),
'content' => $data
)
);
$context = stream_context_create($options);
$url = 'https://restapi.bulksmsonline.com/rest/api/v1/sms/send';
$result = file_get_contents($url,false,$context);
If you are getting token and facing issue in post call only then i have noticed that you have missed a few Request Headers:
like Content-type, Content-length, I have check with the different URLs, It is working for me try it
<?php
$tok=file_get_contents('https://restapi.bulksmsonline.com/rest/api/v1/sms/gettoken/username/xxxx/password/xxxx');
$toke =json_decode($tok, true);
$token=$toke['token'];
$data = json_encode(
array(
'from' => 'TEST',
'to' => '335546821546',
'type'=> 'Text',
'content'=> 'Test',
'sendDateTime'=> '2020/07/07'
)
);
$options = array('http' =>
array(
'method' => 'POST',
'header' => "Content-type: application/json\r\n" .
"Content-length: " . strlen($data) . "\r\n" .
"Authorization Token: " . base64_encode("$token") . "\r\n",
'content' => $data
)
);
$context = stream_context_create($options);
$url = 'https://restapi.bulksmsonline.com/rest/api/v1/sms/send';
$result = file_get_contents($url,false,$context);
var_dump($result);
I have to post some data, but the same adress have some GET and POST functions. My PHP is sending a GET instead of a POST.
$apiURL = 'https://myAPI.com.br/api';
$data = http_build_query(array('postdata' => 10));
$uriRequest = $apiURL.'/main';
$options = array(
"ssl"=>array(
"verify_peer"=>false,
"verify_peer_name"=>false,
),
'https' => array(
'header' => 'Content-type: application/x-www-form-urlencoded',
'method' => 'POST',
'content' => $data
),
);
$context = stream_context_create($options);
$result = file_get_contents($uriRequest, false, $context);
if ($result === FALSE) {
return var_dump($result);
}
return var_dump($result);
I know the ssl part it isnt safe, but it is just for prototyping purpose.
I cant get PHP to POST intestead of GET on the adress 'https://myAPI.com.br/api/main'.
Judging from http://php.net/manual/de/function.stream-context-create.php#74795 the correct way to create a stream context for a https secured page is:
<?php
$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
)
);
As you can see we are using 'http' => array... instead of https.
I am trying to send a file_get_contents request to a URL with POST data and a cookie set.
My code is like that:
$postdata = http_build_query(
array(
'search' => 'test',
'token' => '0'
)
);
// Create a stream
$opts = array(
'http'=> array(
'method'=>"POST",
'content' => $postdata,
'header' => "Content-Type: application/x-www-form-urlencoded\n"."Cookie: session_hash=123456789"
)
);
$context = stream_context_create($opts);
// Open the file using the HTTP headers set above
$file = file_get_contents('https://example.com', false, $context);
echo $file;
This is posting the data as I can see, but the Cookie is not being sent during the request...
I also tried to use the same request but with CURL and I have the same problem with that.
Any help would be appreciated.
Thanks!
The headers may need to be separated by \r\n rather than just \n. You can also use an array, and they'll be sent properly.
'header' => array("Content-Type: application/x-www-form-urlencoded",
"Cookie: session_hash=123456789")
For 2 days I'm having trouble with my PHP script on my server. I've changed nothing and suddenly it didn't work anymore.
Here is the code:
$query = http_build_query($data);
$options = array(
'http' => array(
'header' => "Content-Type: application/x-www-form-urlencoded\r\n".
"Content-Length: ".strlen($query)."\r\n",
'method' => "POST",
'content' => $query,
),
);
$opts = array('http'=>array('header' => "User-Agent:MyAgent/1.0\r\n",'method' => 'POST',
'content' => http_build_query($data),));
$contexts = stream_context_create($opts);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $contexts, -1, 40000);
I'm getting these error messages:
Notice: file_get_contents(): Content-type not specified assuming
application/x-www-form-urlencoded in
Warning: file_get_contents(https://mobile.dsbcontrol.de): failed to open stream: HTTP request failed! HTTP/1.1 500 Internal Server
Error in
But when I try the script locally it works perfectly.
You are passing $contexts to file_get_contents() and that only contains the User-Agent header in the $opts array. All other headers and options are in the $options array which you add in to $context but aren't using. Try:
$query = http_build_query($data);
$options = array(
'http' => array(
'header' => "Content-Type: application/x-www-form-urlencoded\r\n".
"Content-Length: ".strlen($query)."\r\n".
"User-Agent:MyAgent/1.0\r\n",
'method' => "POST",
'content' => $query,
),
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context, -1, 40000);
While the existing answers did not work for me, I managed to solve the problem like this:
The PHP Manual says params must be an associative array in the format $arr['parameter'] = $value. Refer to context parameters for a listing of standard stream parameters.
$header = array(
"Content-Type: application/x-www-form-urlencoded",
"Content-Length: ".strlen($postdata)
);
$packet['method'] = "POST";
$packet['header'] = implode("\r\n", $header);
$packet['content'] = $postdata;
$transmit_data = array('http' => $packet);
$context = stream_context_create($transmit_data);
i'm using this
$url = '';
$result = json_decode(file_get_contents($url, false, stream_context_create(array(
'http' => array(
'method' => 'POST',
'header' => 'Content-type:application/x-www-form-urlencoded',
'content' => http_build_query($dataQuery)
)
))), true);
Hi I am trying to get the following to work. It is making the HTTP request because I can see it at the other end but it is not passing over the two values and parameters.
Can anyone help?
<?php
$url = "http://example.com/ws.php?uid=0000&pin=0000";
$fields = array(
'target1' => $_GET['target1'],
'target2' => $_GET['target2']);
$data = http_build_query($fields);
$context = stream_context_create(array(
'http' => array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $data,
)
));
$result = file_get_contents($url, false, $context);
echo $data."<br />";
echo($result);
?>
If PHP 5.2, see PHP bug https://bugs.php.net/bug.php?id=41051
Description:
the http wrapper ignores 'header' option in stream_create_context( )
if the value is not given as a simple array; neither a string nor a
map (e.g. array('X-My-Header' => 'test')) will cause the additional
headers to be sent.
Based on the examples i've seen you need a \r\n at the end of the header value.
'header' => "Content-type: application/x-www-form-urlencoded\r\n"
apart from that you will also need to provide
"Content-Length: " . strlen($data) . "\r\n",