PHP - how to grab website html from a form post submission

Aleran

Registered
In my php script I need to be able to send form data (of method post) to a cgi script and then have my php grab the html contents of what the cgi script returns. Does anyone know how to do this? Thanks.
 
Why not use PHP to grab the form data directly? Or is whatever the cgi is doing not possible to do with PHP?
 
Well the output from the CGI is not just a representation of the form data. The CGI is actually a seach engine of sorts so I need to be able to grab the html of what the CGI search engine returns.
 
I just found out how to do it. In case anyone else is interested I've posted the function I found on the net to accomplish it.


/* sendToHost
* ~~~~~~~~~~
* Params:
* $host - Just the hostname. No http:// or
/path/to/file.html portions
* $method - get or post, case-insensitive
* $path - The /path/to/file.html part
* $data - The query string, without initial question mark
* $useragent - If true, 'MSIE' will be sent as
the User-Agent (optional)
*
* Examples:
* sendToHost('www.google.com','get','/search','q=php_imlib');
* sendToHost('www.example.com','post','/some_script.cgi',
* 'param=First+Param&second=Second+param');
*/

function sendToHost($host,$method,$path,$data,$useragent=0)
{
// Supply a default method of GET if the one passed was empty
if (empty($method)) {
$method = 'GET';
}
$method = strtoupper($method);
$fp = fsockopen($host, 80);
if ($method == 'GET') {
$path .= '?' . $data;
}
fputs($fp, "$method $path HTTP/1.1\r\n");
fputs($fp, "Host: $host\r\n");
fputs($fp,"Content-type: application/x-www-form- urlencoded\r\n");
fputs($fp, "Content-length: " . strlen($data) . "\r\n");
if ($useragent) {
fputs($fp, "User-Agent: MSIE\r\n");
}
fputs($fp, "Connection: close\r\n\r\n");
if ($method == 'POST') {
fputs($fp, $data);
}

while (!feof($fp)) {
$buf .= fgets($fp,128);
}
fclose($fp);
return $buf;
}



Found it from http://www.faqts.com/knowledge_base/view.phtml/aid/12039/fid/51


The function returns the html returned by the script so if you just wanted to see it just echo the function call.
 
or if the cgi program you have accepts GET params you could use file(), or file_get_contents()


PHP:
$result = file('http://whatever.com/search.cgi?param=' . urlencode($_POST['param']));

assuming your php's allow_url_fopen is 1
 
Back
Top