PHP向特定URL发送POST数据

方法一:HTTP函数发送方式

说明:$data为POST发送的数据:$key为参数名,$val为参数值

 

  1. $URL = "http://pre.payment.sdoa.sdo.com/";
  2. $data = $key1."=".val1."&".$key2."=".val2;
  3. $PostResult = http_post_data($BGWURL,$data );

 

 

方法二:Curl Post数据

  1. function _curl_post($url, $vars) {
  2.     $ch = curl_init();
  3.     curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
  4.     curl_setopt($ch, CURLOPT_URL,$url);
  5.     curl_setopt($ch, CURLOPT_POST, 1 );
  6.     curl_setopt($ch, CURLOPT_HEADER, 0 ) ;
  7.     curl_setopt($ch, CURLOPT_POSTFIELDS, "var=".$vars);
  8.     $data = curl_exec($ch);
  9.     curl_close($ch);
  10.     if ($data)
  11.         return $data;
  12.     else
  13.         return false;
  14. }

 

 

方法三:fsockopen方式

说明:$data为POST发送的数据:$data为数组形式

 

  1. function posttohost($url, $data) {
  2.     $url = parse_url($url);
  3.     if (!$url) return "couldn't parse url";
  4.     if (!isset($url['port'])) { $url['port'] = ""; }
  5.     if (!isset($url['query'])) { $url['query'] = ""; }
  6.     $encoded = "";
  7.     while (list($k,$v) = each($data)) {
  8.         $encoded .= ($encoded ? "&" : "");
  9.         $encoded .= rawurlencode($k)."=".rawurlencode($v);
  10.     }
  11.     $fp = fsockopen($url['host'], $url['port'] ? $url['port'] : 80);
  12.     if (!$fp) return "Failed to open socket to $url[host]";
  13.     fputs($fp, sprintf("POST %s%s%s HTTP/1.0/n", $url['path'], $url['query'] ? "?" : "", $url['query']));
  14.     fputs($fp, "Host: $url[host]/n");
  15.     fputs($fp, "Content-type: application/x-www-form-urlencoded/n");
  16.     fputs($fp, "Content-length: " . strlen($encoded) . "/n");
  17.     fputs($fp, "Connection: close/n/n");
  18.     fputs($fp, "$encoded/n");
  19.     $line = fgets($fp,1024);
  20.     if (!eregi("^HTTP/1/.. 200", $line)) return;
  21.     $results = ""; $inheader = 1;
  22.     while(!feof($fp)) {
  23.         $line = fgets($fp,1024);
  24.         if ($inheader && ($line == "/n" || $line == "/r/n")) {
  25.             $inheader = 0;
  26.         }
  27.         elseif (!$inheader) {
  28.             $results .= $line;
  29.         }
  30.     }
  31.     fclose($fp);
  32.     return $results;
  33. }

 

0

这篇文章还没有评论

发表评论