乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      PHP的三種HTTP請求,php模擬post 提交

       mrjbydd 2011-12-14
      PHP的三種HTTP請求,php模擬post 提交
      2011-05-25 17:28

      方法一:利用php的socket編程來直接給接口發(fā)送數(shù)據(jù)來模擬post的操作。

      建立兩個文件post.php,getpost.php
      post.php內(nèi)容如下:
      <?php
       $flag = 0;
       $post = '';
       $errno = '';
       $errstr = '';
       //要post的數(shù)據(jù)
      $argv = array(
          'var1'=>'abc',
          'var2'=>'how are you , my friend??'

      );
      //構(gòu)造要post的字符串
      foreach ($argv as $key=>$value) {
          if ($flag!=0) {
              $post .= "&";
              $flag = 1;
          }
          $post.= $key."="; $post.= urlencode($value);
          $flag = 1;
          }
          $length = strlen($post);
           //創(chuàng)建socket連接
         $fp = fsockopen("localhost",81,$errno,$errstr,10) or exit($errstr."--->".$errno);
          //構(gòu)造post請求的頭
          $header  = "POST /flandy/getpost.php HTTP/1.1\r\n";
          $header .= "Host:127.0.0.1\r\n";
          $header .= "Referer:/flandy/post.php\r\n";
          $header .= "Content-Type: application/x-www-form-urlencoded\r\n";
          $header .= "Content-Length: ".$length."\r\n";
          $header .= "Connection: Close\r\n\r\n";
          //添加post的字符串
          $header .= $post."\r\n";
          

          //發(fā)送post的數(shù)據(jù)
         fputs($fp,$header);
          $inheader = 1;
          while (!feof($fp)) {
              $line = fgets($fp,1024); //去除請求包的頭只顯示頁面的返回數(shù)據(jù)
              if ($inheader && ($line == "\n" || $line == "\r\n")) {
                   $inheader = 0;
              }
              if ($inheader == 0) {
                echo $line;
              }
          }

      fclose($fp);
      ?>
      getpost.php的內(nèi)容如下
      <?php
      echo "this is the data posted";
      echo "<pre>";
      print_r($_REQUEST);
      echo "</pre>";
      ?>
      結(jié)果輸出:
      this is the data posted


      Array(
      [var1] => abc
      [var2] => how are you , my friend??
      )



      方法二:
      使用PHP的curl擴展或HttpClient.class.php類,這兩個非常類似,下面簡單的列出curl的實現(xiàn)代碼。

      兩個文件post2.php和getpost2.php
      post2.php的內(nèi)容如下:
      <?php
      $psecode = ’NDE005’;
      $website = ’www.baidu.com’;
      $amt = 1;
      $pwd = 123456;
      $ch = curl_init();
      $curl_url = "http://localhost:81/flandy/getpost2.php?web=" . $website .
      "&pwd=" . $pwd . "&action=check&pseid=" . $psecode .
      "&amt=" . $amt;
      curl_setopt($ch, CURLOPT_URL, $curl_url);
      curl_setopt($ch, CURLOPT_POST, 1);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//不直接輸出,返回到變量
      $curl_result = curl_exec($ch);
      $result = explode(',', $curl_result);
      curl_close($ch);
      print_r($result);
      ?>
      getpost2.php的內(nèi)容如下:
      <?php
      echo "returndata<br>";
      echo "<pre>";
      print_r($_REQUEST);
      echo "</pre>";
      ?>
      結(jié)果輸出:
      Array (
        [0] => returndata
        Array(
          [web] => ’wwwbaiducom’
          [pwd] => 123456
          [action] => check
          [pseid] => ’NDE005’
          [amt] =>
       )
      )

      方法三:這個要借助第三方類庫HttpClient可以到這里下載:http://scripts./httpclient/<?php
      require_once 'HttpClient.class.php’;
      $params = array(’web’ => ’www.baidu.com’,
      ’pwd’ => ’123456’,
      ’action’ => ’check’,
      ’pseid’ => ’NDE005’,
      ’amt’ => 1);
      $pageContents = HttpClient::quickPost(’http://localhost:81/flandy/getpost3.php’, $params);
      $result = explode(’,’, $pageContents);
      print_r($result);



      補充:
      HTTP請求常用方案有以下(友情提示:排列順序只為所想起時的先后順序并再無特別含義)
      1.HttpClient 
      Version 0.9, Simon Willison April 6th 2003
      http://scripts./httpclient/HttpClient.class.php

      2.snoopy
      Snoopy Snoopy 1.2.3 November 7, 2005 
      http://snoopy./

      3.pear::http_client
      1.1.0 (stable) was released on 2006-06-03
      http://pear./package/HTTP_Client

      4.curl or php_curl
      5.wget
      6.php_socket


      前3個算是比較完整的類,所以后面的暫時不考慮了。
      庫的選擇一般原則是找用的人多,更新持久的,因此 pear::http_client 一馬當先,但這個必須是統(tǒng)籌在PEAR之下,因為要用到一些PEAR的輔助類,不是很適合單獨使用,請回去等錄用通知吧。這回合Snoopy 領(lǐng)先一步,但粗略一看核心文件Snoopy.class.php 體重38KB,再看 HttpClient 感覺是相當苗條了,核心文件 HttpClient.class.php 占地12KB,這回合 HttpClient 也得一分,不過最后更新日期讓人看得心寒。

      人氣測試(pear::http_client友情出場):
      1.Google Trends 
      結(jié)果:放棄。
      因為 Snoopy 在某個世界實在太有名氣了,而且"http client" 關(guān)鍵字也太含糊。

      2.Google Code Search
      規(guī)則:php + 包含類名的一行并用雙引號括起來
      HttpClient 100
      http://www.google.com/codesearch ... class+HttpClient"

      Snoopy 100
      http://www.google.com/codesearch ... +"class+Snoopy"

      pear::http_client 12 (還是請繼續(xù)回去等通知吧)
      http://www.google.com/codesearch ... "&btnG=Search

      核心PK:
      一般來說,php 的HTTP CLIENT都是通過PHP_CURL或者PHP_SOCKET來實現(xiàn)的,所以這局應(yīng)該又是平手。

      功能PK:
      一般來說功能與體重成正比,所以 Snoopy 的給人的第一印象還是很令人得期待的。(考察未完成)

        本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
        轉(zhuǎn)藏 分享 獻花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多