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",
Related
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")
I'm trying to send a special character, such as ñ, through a POST request in PHP. When I do it, it comes out as ñ, what is wrong and how do I fix it?
I'm sending and receiving the post request in PHP, here is what I use to send it:
$url = '<url>';
$data = array('key' => 'ñ');
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data)
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
echo $result;
Thanks in advance!
Try using this:
$data = array('key' => urlencode('ñ'));
And in the $url file:
$_POST['key']=urldecode($_POST['key']);
This is how I use to send special characters in GET and POST method with ajax, it must work for php too.
$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);
I have question, is it possible to first post data to a site and then get the source code of the site I was posting the data to?
I tried with this:
$html = file_get_contents('http://test.com/test.php?test=test');
But the ?test=test is $_GET....
So yeah, I hope someone can help me! :) Thanks in advance (Sorry for bad english!).
You can use 3rd parameter of this function: context
$postdata = http_build_query(
array(
'var1' => 'some content',
'var2' => 'doh'
)
);
$opts = array(
'http' => array(
'method' => "POST",
'header' => "Connection: close\r\n".
"Content-Length: ".strlen($postdata)."\r\n",
'content' => $postdata
)
);
$context = stream_context_create($opts);
$result = file_get_contents('http://example.com/submit.php', false, $context);
// edit -little bug should be:
$opts = array(
'http' => array(
'method' => "POST",
'header' => "Connection: close\r\n".
"Content-type: application/x-www-form-urlencoded\r\n".
"Content-Length: ".strlen($postdata)."\r\n",
'content' => $postdata
)
);
You can not get the source, but you can get the page/output as it is provided by the server. As you mention file_get_contents(), you can use it to send a POST-request, but it would look something like this.
// Create map with request parameters
$params = array ('surname' => 'Filip', 'lastname' => 'Czaja');
// Build Http query using params
$query = http_build_query ($params);
// Create Http context details
$contextData = array (
'method' => 'POST',
'header' => "Connection: close\r\n".
"Content-Length: ".strlen($query)."\r\n",
'content'=> $query );
// Create context resource for our request
$context = stream_context_create (array ( 'http' => $contextData ));
// Read page rendered as result of your POST request
$result = file_get_contents (
'http://www.sample-post-page.com', // page url
false,
$context);
// Server response is now stored in $result variable so you can process it
Example from: http://fczaja.blogspot.se/2011/07/php-how-to-send-post-request-with.html