I got a homework to retrieve the 2nd results page of http://wiki.webo-facto.com from a search post data. Like this:
$postdata = http_build_query(
array(
'q' => 'catalogue',
'submit' => 'searchbutton'
)
);
$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://wiki.webo-facto.com/', false, $context);
No problem.
My sensei said that:"Once a search is done, the search criteria is stored in session. It is this session that makes navigation work".
Then, from the same script i add $_SESSION['post']=$postdata;
header('Location: getsecondpage.php')
All of this stored in poster.php .
Now Time to retrive the second page in getsecondpage.php:
session_start();
$postdata = $_SESSION['postdata'];
$opts2 = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $postdata
)
);
$context2 = stream_context_create($opts2);
$result2 = file_get_contents('http://wiki.webo-facto.com/resultspage-2.html', false, $context2);
echo $result2;
return the results of the 1st page that are not what i want.
Your suggestion will be very helpful.
Notice: that i have also start the session from poster.php.(code not visible).
Sorry for my bad english, Im franco-saxon.
Your sensei is right, the search criteria is stored in session BUT in REMOTE SERVER SESSION. So what you have to do is "tell the server your session cookie" to be able to save and load it!
So for the first query we will do a POST with our search criteria AND telling the remote server our session name and id:
$opts = array('http' =>
array(
'method' => 'POST',
'header' => "Content-type: application/x-www-form-urlencoded\r\n" .
"Cookie: ".session_name()."=".session_id()."\r\n",
'content' => $postdata
)
);
And for the second one, server already stored our query in SESSION so we do not need to send it again. So we'll do a GET. Also telling server our session name and session id.
$opts2 = array('http' =>
array(
'method' => 'GET',
'header' => "Content-type: application/x-www-form-urlencoded\r\n" .
"Cookie: ".session_name()."=".session_id()."\r\n",
)
);
And that's it!
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 need to submit a form to a website (http://www.itu.int/online/mms/mars/ship_search.sh) and I use the following code:
$postdata = http_build_query(
array(
'sh_mmsi' => '246709000'
)
);
$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://www.itu.int/online/mms/mars/ship_search.sh', false, $context);
For some reason is not triggering the search query. If I hit the Submit button in the URL address http://www.itu.int/online/mms/mars/ship_search.sh?sh_mmsi=246709000, then the results shows at the end. I wonder if the problem is that this website has two forms and I would need to specify which one to post to? If so, how should I do it?
I've been looking for a solution for such a long time for my issue.
But i just can't find any solutions and i'm not sure what exactly to search for.
I'll explain my problem here.
Currently i have this PHP script which works perfect and sends a successfully request and receives the respond.
<?php
$url = 'http://example.nl/index.php';
$data = array('url' => 'http://www.myip.nl/', 'verkort' => 'Verkort URL');
// use key 'http' even if you send the request to https://...
$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);
var_dump($result);
?>
But i really need help with sending a request with the following content:
See screenshot: http://i.imgur.com/VgedJes.png
I need to add the 3 headers and i'm not sure how to tell a field/value is empty as you can see in the screenshot.
I just can't figure out how to do this.
I would really appreciate it if someone could help me out with this!
To Add Header :
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\nX-Requested-With: XmlHttpRequest",
'method' => 'POST',
'content' => http_build_query($data),
),
);
Keep in mind that X-Requested-Header can only be interpreted by your PHP application
To send a parameter with an empty value you can do as follow
$data = array('url' => 'http://www.myip.nl/', 'verkort' => 'Verkort URL', 'notMandatoryParam' => '');
Also in your PHP code, you must do a verification : if(empty($_POST['notMandatoryParam'])) {/*SKIP ME*/}
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
Hi I'm trying to send post values using a PHP script to an external website and get the result. My code is as follows
$postdata = http_build_query(
array(
'drpservice' => 091,
'drpdirection' => 1,
'drpbusstop' => 18051
)
);
$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($url, false, $context);
echo $result;
However, it won't return the required results unless I can manage to send in the hidden _VIEWSTATE value.
Anyone able to help?
You need to first get the form page using a GET request, and then you can parse the page to get the _VIEWSTATE value using a regular expression.
Then when doing the POST pass the value you got for it.