<?
// Sends GET or POST, with data, to URL
// $form_url:    full URI, http://www.isp.com/directory/directory
// $form_method: GET or POST
// $form_data:   associative array of data for GET or POST
// $ShowRequest: if set, return the request sent to server
//      Request text enclosed by <request></request>
// Returns:  string with data received from server

function GetPost( $form_url, $form_method, $form_data, $ShowRequest=NULL )
{
// Build the request data string
$request_data = "";
if( isset($form_data) )
foreach ($form_data as $name => $value)
$request_data .= $name.'='.urlencode($value)."&";
$request_data = substr($request_data,0,-1);

// Build the request
$url         = parse_url($form_url);
$form_method = strtoupper($form_method);
$request     = $form_method." ";
switch ($form_method)
{
   case 'GET': 
      $request .= $url['path']."?".$request_data." HTTP/1.0\r\n".
                  "Host: ".$url['host']."\r\n\r\n";
      break;
   case 'POST': 
      $request .= $url['path']." HTTP/1.0\r\n".
                  "Host: ".$url['host']."\r\n".
                  "Content-type: application/x-www-form-urlencoded\r\n".
                  "Content-length: ".strlen($request_data)."\r\n\r\n".
                  $request_data;
      break;
   default:
      die("Invalid method: ".$form_method);
}

// Open the connection 
$fp = fsockopen($url['host'], 80, $err_num, $err_msg, 30); 
if ($fp) 
{
if( isset($ShowRequest) ) $response = "<request>$request</request>\n";
   // Submit form
   fputs($fp, $request);
   
   // Get the response 
   while (!feof($fp)) 
     $response .= fgets($fp, 1024);
   fclose($fp);
   
   return $response;
}
else
{
   echo 'Could not connect to: '.htmlentities($url['host']).'<br />';
   echo 'Error #'.$err_num.': '.$err_msg;
   exit;
}
}

//EXAMPLE
$mydata['q']     = 'php'; 
$mydata['start'] = '10'; 
$qq = GetPost("http://www.google.com/search","GET",$mydata);
print $qq;
?>


----- Original Message ----- 
From: "Wei Weng" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Tuesday, October 29, 2002 4:44 PM
Subject: [PHP] HTML Post


How do you do a HTML post in php?

Thanks

-- 
Wei Weng
Network Software Engineer
KenCast Inc.



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to