php-post類比(摘自網路)

來源:互聯網
上載者:User

標籤:blog   http   io   os   ar   for   strong   資料   sp   

這也是個老生常談的話題了,上午花了點時間把這個問題整理了一下。

一般來說用PHP來類比post提交資料有三種方法,file_get_contents、curl和socket。

寫了個公用函數,專門用來列印post資料:

 

[php] view plaincopy 
  1. <?php  
  2. function pr() {  
  3.     $params = func_get_args();  
  4.     foreach ($params as $key => $value) {  
  5.         echo "<pre>";  
  6.         print_r($value);  
  7.         echo "</pre>";  
  8.     }  
  9. }  


先寫一個post.php,用來接收post資料並列印出來:

 

 

[php] view plaincopy 
  1. <?php  
  2. require dirname(__FILE__).‘/function.php‘;  
  3.   
  4. if (isset($_POST) && !empty($_POST)) {  
  5.     pr($_POST);  
  6. } else {  
  7.     pr("NO POST DATA!");  
  8. }  


下面是用file_get_contents來類比post:

 

 

[php] view plaincopy 
  1. <?php  
  2. require dirname(__FILE__).‘/function.php‘;  
  3.   
  4. function file_get_contents_post($url, $post) {  
  5.     $options = array(  
  6.         ‘http‘ => array(  
  7.             ‘method‘ => ‘POST‘,  
  8.             // ‘content‘ => ‘name=caiknife&[email protected]‘,  
  9.             ‘content‘ => http_build_query($post),  
  10.         ),  
  11.     );  
  12.   
  13.     $result = file_get_contents($url, false, stream_context_create($options));  
  14.   
  15.     return $result;  
  16. }  
  17.   
  18. $data = file_get_contents_post("http://www.a.com/post/post.php", array(‘name‘=>‘caiknife‘, ‘email‘=>‘[email protected]‘));  
  19.   
  20. var_dump($data);  


很簡單是吧?再來看看curl類比post:

 

 

[php] view plaincopy 
  1. <?php  
  2. require dirname(__FILE__).‘/function.php‘;  
  3.   
  4. function curl_post($url, $post) {  
  5.     $options = array(  
  6.         CURLOPT_RETURNTRANSFER => true,  
  7.         CURLOPT_HEADER         => false,  
  8.         CURLOPT_POST           => true,  
  9.         CURLOPT_POSTFIELDS     => $post,  
  10.     );  
  11.   
  12.     $ch = curl_init($url);  
  13.     curl_setopt_array($ch, $options);  
  14.     $result = curl_exec($ch);  
  15.     curl_close($ch);  
  16.     return $result;  
  17. }  
  18.   
  19. $data = curl_post("http://www.a.com/post/post.php", array(‘name‘=>‘caiknife‘, ‘email‘=>‘[email protected]‘));  
  20.   
  21. var_dump($data);  


最後是用socket來類比post:

 

 

[php] view plaincopy 
  1. <?php  
  2. require dirname(__FILE__).‘/function.php‘;  
  3.   
  4. function socket_post($url, $post) {  
  5.     $urls = parse_url($url);  
  6.     if (!isset($urls[‘port‘])) {  
  7.         $urls[‘port‘] = 80;  
  8.     }  
  9.   
  10.     $fp = fsockopen($urls[‘host‘], $urls[‘port‘], $errno, $errstr);  
  11.     if (!$fp) {  
  12.         echo "$errno, $errstr";  
  13.         exit();  
  14.     }  
  15.   
  16.     $post = http_build_query($post);  
  17.     $length = strlen($post);  
  18.     $header = <<<HEADER  
  19. POST {$urls[‘path‘]} HTTP/1.1  
  20. Host: {$urls[‘host‘]}  
  21. Content-Type: application/x-www-form-urlencoded  
  22. Content-Length: {$length}  
  23. Connection: close  
  24.   
  25. {$post}  
  26. HEADER;  
  27.   
  28.     fwrite($fp, $header);  
  29.     $result = ‘‘;  
  30.     while (!feof($fp)) {  
  31.         // receive the results of the request  
  32.         $result .= fread($fp, 512);  
  33.     }  
  34.     $result = explode("\r\n\r\n", $result, 2);  
  35.     return $result[1];  
  36. }  
  37.   
  38. $data = socket_post("http://www.a.com/post/post.php", array(‘name‘=>‘caiknife‘, ‘email‘=>‘[email protected]‘));  
  39.   
  40. var_dump($data);  


這三種方法最後看到的內容都是一樣的,但是在是用socket的時候,發送header資訊時必須要注意header的完整資訊,比如content type和content length必須要有,connection: close和post資料之間要空一行,等等;而通過socket取得的內容是包含了header資訊的,要處理一下才能獲得真正的內容。

php-post類比(摘自網路)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.