How can I post data and get the results from the action page with PHP?
I have a web page called ABC. This web page contains a form with just one input, "ID". When you type your ID and submit it, it sends your ID to the server and prints your information on another page.
I know the form page's web address but not the result page's.
I want to post an ID to that form from my script and get the result page into a variable.
The code so far that I tried:
function getUrl($url, $method='', $vars=''){ $ch = curl_init(); if ($method == 'post') { $str = ""; foreach( $vars as $var => $ele){ $str .= $var; $str .= "="; $str .= $ele; $str .= "&"; } curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $str); } curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_COOKIEJAR, 'asd.txt'); curl_setopt($ch, CURLOPT_COOKIEFILE, 'asd.txt'); curl_setopt($ch, CURLOPT_TIMEOUT, 50000); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 50000); $buffer = curl_exec($ch); curl_close($ch); unset($ch); return $buffer; } $loginUrl = 'http://app2.sgk.gov.tr/AylikHesap/servlet/'; $loginFields = array('mernisno'=>'xxx'); $remotePageUrl = 'http://app2.sgk.gov.tr/AylikHesap/servlet/com.sgk.aylikhesap.servlet.EventMultiplexer'; $login = getUrl($loginUrl, 'post', $loginFields); $remotePage = getUrl($remotePageUrl); echo $remotePage;
It should bring the results. xxx is the ID that I'm talking about.
How do I do that?
Answers
You can use Zend_Http_Client for this
$client = new Zend_Http_Client(); $client->setUri('http://example.com/login.php'); $client->setParameterPost('ID', 1234); $response = $client->request('POST'); $result = $response->getBody();
If you do not want to use Zend Framework for any reason, you can use this function.
You can retrieve data from a site, but most site's implement some sort of token validation, so in other words you can't post form data across domains.