欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

PHP中使用socket方式GET、POST數(shù)據(jù)實(shí)例

 更新時(shí)間:2015年04月02日 09:26:45   投稿:junjie  
這篇文章主要介紹了PHP中使用socket方式GET、POST數(shù)據(jù)實(shí)例,本文分別給出GET方式和POST方式的代碼實(shí)例,需要的朋友可以參考下

1. 使用 PHP 獲得網(wǎng)頁內(nèi)容 GET方式

復(fù)制代碼 代碼如下:

<?php
function socketGet($url, &$ret)
{
 $urlArr = parse_url($url);
 $host = $urlArr['host'];
 $port = isset($urlArr['port'])?$urlArr['port']:80;
 $path = isset($urlArr['path'])?$urlArr['path']:"/";
 $fp = fsockopen($host, $port, $errno, $errstr, 30);
 if (!$fp)
 {
  echo "$errstr ($errno)<br />\n";
  return false;
 }
 else
 {
     $out = "GET $path HTTP/1.1\r\n";
     $out .= "Host: $host\r\n";
     $out .= "Connection: Close\r\n\r\n";
  $ret = "";
     fwrite($fp, $out);
     while (!feof($fp))
  {
         $ret .= fgets($fp, 128);
     }
     fclose($fp);
 }
 return true;
}
?>

2. 使用 PHP 向頁面 POST 數(shù)據(jù)

復(fù)制代碼 代碼如下:

<?php
function socketPost($url, $data, &$ret)
{
 $urlArr = parse_url($url);
 $host = $urlArr['host'];
 $port = isset($urlArr['port'])?$urlArr['port']:80;
 $path = isset($urlArr['path'])?$urlArr['path']:"/";
 $fp = fsockopen($host, $port, $errno, $errstr, 30);
 if (!$fp)
 {
     echo "$errstr ($errno)<br />\n";
  return false;
 }
 else
 {
     $out = "POST $path HTTP/1.1\r\n";
     $out .= "Host: $host\r\n";
  $out .= "Content-Type: application/x-www-form-urlencoded\r\n";
  $out .= "Content-Length: ".strlen($data)."\r\n";
     $out .= "Connection: Keep-Alive\r\n\r\n";
  $out .= $data;
  $ret = "";
     fwrite($fp, $out);
     while (!feof($fp))
  {
         $ret .= fgets($fp, 128);
     }
     fclose($fp);
 }
 return true;
}
?>

如果post報(bào)錯(cuò),把$out .= "Connection: Keep-Alive\r\n\r\n";中的Keep-Alive改成Close

相關(guān)文章

最新評(píng)論