目錄
0. 引言 1. file_get_contents版本 2. Socket版本 3. Curl版本 4. Curl版本(2) 5. 模擬檔案上傳
0. 引言
本文總結了通過PHP程式碼方式模擬各種HTTP請求
1. file_get_contents版本
<?php /** * 傳送post請求 * @param string $url 請求地址 * @param array $post_data post鍵值對資料 * @return string */ function send_post($url, $post_data) { //使用給出的關聯(或下標)陣列生成一個經過 URL-encode 的請求字串 $postdata = http_build_query($post_data); $options = array( 'http' => array( 'method' => 'POST', 'header' => 'Content-type:application/x-www-form-urlencoded', 'content' => $postdata, 'timeout' => 15 * 60 // 超時時間(單位:s) ) ); //建立並返回一個資源流上下文,該資源流中包含了 options 中提前設定的所有引數的值 $context = stream_context_create($options); $result = file_get_contents($url, false, $context); return $result; } $post_data = array( 'username' => 'zhenghan', 'password' => '111' ); $result = send_post('http://localhost/test/index.php', $post_data); echo $result; ?>
Relevant Link:
http://php.net/manual/zh/function.http-build-query.php http://php.net/manual/zh/function.stream-context-create.php
2. Socket版本
<?php /** * Socket版本 */ function request_by_socket($remote_server, $remote_path, $post_string, $port = 80, $timeout = 30) { $socket = fsockopen($remote_server, $port, $errno, $errstr, $timeout); if (!$socket) { die("$errstr($errno)"); } fwrite($socket, "POST $remote_path HTTP/1.0"); fwrite($socket, "User-Agent: Socket Example"); fwrite($socket, "HOST: $remote_server"); fwrite($socket, "Content-type: application/x-www-form-urlencoded"); fwrite($socket, "Content-length: " . (strlen($post_string) + 8) . ""); fwrite($socket, "Accept:*/*"); fwrite($socket, ""); fwrite($socket, "mypost=$post_string"); fwrite($socket, ""); $header = ""; while ($str = trim(fgets($socket, 4096))) { $header .= $str; } $data = ""; while (!feof($socket)) { $data .= fgets($socket, 4096); } return $data; } $post_string = "app=socket&version=beta"; $result = request_by_socket('localhost', '/test.php', $post_string); echo $result; ?>
Relevant Link:
http://php.net/manual/zh/function.fsockopen.php
3. Curl版本
<?php /** * Curl版本 */ function request_by_curl($remote_server, $post_string) { //初始化一個新的會話,返回一個cURL控制程式碼,供curl_setopt(), curl_exec()和curl_close() 函式使用。 $ch = curl_init(); //curl_setopt — 設定一個cURL傳輸選項 curl_setopt($ch, CURLOPT_URL, $remote_server); curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_USERAGENT, "http://littlehann.cnblogs.com CURL Example beta"); //curl_exec — 執行一個cURL會話,這個函式應該在初始化一個cURL會話並且全部的選項都被設定後被呼叫。 $data = curl_exec($ch); //關閉一個cURL會話並且釋放所有資源。cURL控制程式碼ch 也會被釋放。 curl_close($ch); return $data; } $post_string = "app=request&version=beta"; $result = request_by_curl('http://localhost/test.php', $post_string); echo $result; ?>
Relevant Link:
http://php.net/manual/zh/function.curl-init.php http://php.net/manual/zh/function.curl-setopt.php http://php.net/manual/zh/function.curl-exec.php http://blog.51yip.com/php/1039.html
4. Curl版本(2)
<?php /** * 傳送HTTP請求 * * @param string $url 請求地址 * @param string $method 請求方式 GET/POST * @param string $refererUrl 請求來源地址 * @param array $data 傳送資料 * @param string $contentType * @param string $timeout * @param string $proxy */ function send_request($url, $data, $refererUrl = '', $method = 'GET', $contentType = 'application/json', $timeout = 30, $proxy = false) { $ch = null; if('POST' === strtoupper($method)) { $ch = curl_init($url); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_HEADER,0 ); curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_FORBID_REUSE, 1); curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); if ($refererUrl) { curl_setopt($ch, CURLOPT_REFERER, $refererUrl); } if($contentType) { curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:'.$contentType)); } if(is_string($data)) { curl_setopt($ch, CURLOPT_POSTFIELDS, $data); } else { curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data)); } } else if('GET' === strtoupper($method)) { if(is_string($data)) { $real_url = $url. (strpos($url, '?') === false ? '?' : ''). $data; } else { $real_url = $url. (strpos($url, '?') === false ? '?' : ''). http_build_query($data); } $ch = curl_init($real_url); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:'.$contentType)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); if ($refererUrl) { curl_setopt($ch, CURLOPT_REFERER, $refererUrl); } } else { //返回一個陣列,其中每個元素都是目前使用者自定義函式的引數列表的相應元素的副本 $args = func_get_args(); return false; } if($proxy) { curl_setopt($ch, CURLOPT_PROXY, $proxy); } $ret = curl_exec($ch); //獲取最後一次傳輸的相關資訊。 $info = curl_getinfo($ch); $contents = array( 'httpInfo' => array( 'send' => $data, 'url' => $url, 'ret' => $ret, 'http' => $info ) ); curl_close($ch); return $contents; } $data = array(1 => "hello world!"); $r_url = "http://localhost/test.php"; $result = send_request($r_url, json_encode($data), NULL, 'POST'); echo $result; ?>
Relevant Link:
http://php.net/manual/zh/function.curl-getinfo.php http://blog.snsgou.com/post-161.html http://www.cnblogs.com/simpman/p/3549816.html
5. 模擬檔案上傳
sender.php
<?php function getFileList($directory) { $files = array(); if(is_dir($directory)) { if($dh = opendir($directory)) { while(($file = readdir($dh)) !== false) { if($file != '.' && $file != '..' && $file !== "rule.php" && $file !== "filter.php" && $file !== "vul_rules.json" && $file !== "bad" && $file !== "good") { $files[] = $file; } } closedir($dh); } } return $files; } /** * 傳送post請求 * @param $post_data 待上傳檔案 * @param $post_url 檔案上傳地址 * @return string */ function file_post($post_data, $post_url) { $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $post_url); curl_setopt($curl, CURLOPT_POST, 1 ); curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl,CURLOPT_USERAGENT,"Mozilla/4.0"); $result = curl_exec($curl); $error = curl_error($curl); return $error ? $error : $result; } ///遍歷bad目錄,逐個檔案上傳 $files = getFileList("./bad"); foreach ($files as $key => $value) { //上傳檔案 $url = "http://localhost/test/index.php?action=uploadfixfile"; $data = array( "file" => "@" . dirname(__FILE__) . "\\bad\\$value" ); $result .= file_post($data, $url); } echo $result; ?>
receiver.php
<?php if ($_GET['action'] == "uploadfixfile") { if($_FILES) { $filename = $_FILES['file']['name']; $tmpname = $_FILES['file']['tmp_name']; $newname = dirname(__FILE__) . '\\fix\\' . $filename; //die(var_dump($newname)); if(move_uploaded_file($tmpname, $newname)) { echo "$filename 上傳成功" . "</br>"; } else { echo "$filename 上傳失敗" . "</br>"; } } } ?>
Relevant Link:
http://flashphp.org/blog/2010/03/php%E4%BD%BF%E7%94%A8curl%E4%B8%8A%E4%BC%A0%E6%96%87%E4%BB%B6%E7%9A%84%E5%87%BD%E6%95%B0/ http://book.51cto.com/art/201404/437024.htm http://my.oschina.net/adamboy/blog/54436
Copyright (c) 2014 LittleHann All rights reserved