file_get_contents版本:
1 /** 2 * 发送post请求 3 * @param string $url 请求地址 4 * @param array $post_data post键值对数据 5 * @return string 6 */ 7 function send_post($url, $post_data) { 8 9 $postdata = http_build_query($post_data);10 $options = array(11 'http' =>; array(12 'method' =>; 'post',13 'header' =>; 'content-type:application/x-www-form-urlencoded',14 'content' =>; $postdata,15 'timeout' =>; 15 * 60 // 超时时间(单位:s)16 )17 );18 $context = stream_context_create($options);19 $result = file_get_contents($url, false, $context);20 21 return $result;22 }
使用如下:
1 post_data = array(2 'username' => 'stclair2201',3 'password' => 'handan'4 );5 send_post('http://blog.snsgou.com', $post_data);
实战经验:
当我利用上述代码给另一台服务器发送http请求时,发现,如果服务器处理请求时间过长,本地的php会中断请求,即所谓的超时中断,第一个怀疑的是php本身执行时间的超过限制,但想想也不应该,因为老早就按照这篇文章设置了“php执行时间限制”(【推荐】php上传文件大小限制大全 ),仔细琢磨,想想,应该是http请求本身的一个时间限制,于是乎,就想到了怎么给http请求时间限制搞大一点。。。。。。查看php手册,果真有个参数 “ timeout ”,默认不知道多大,当把它的值设大一点,问题得已解决,弱弱地做个笔记~~~
socket版本:
1 /** 2 * socket版本 3 * 使用方法: 4 * $post_string = app=socket&version=beta; 5 * request_by_socket('blog.snsgou.com', '/restserver.php', $post_string); 6 */ 7 function request_by_socket($remote_server,$remote_path,$post_string,$port = 80,$timeout = 30) { 8 $socket = fsockopen($remote_server, $port, $errno, $errstr, $timeout); 9 if (!$socket) die($errstr($errno));10 fwrite($socket, post $remote_path http/1.0);11 fwrite($socket, user-agent: socket example);12 fwrite($socket, host: $remote_server);13 fwrite($socket, content-type: application/x-www-form-urlencoded);14 fwrite($socket, content-length: . (strlen($post_string) + 8) . );15 fwrite($socket, accept:*/*);16 fwrite($socket, );17 fwrite($socket, mypost=$post_string);18 fwrite($socket, );19 $header = ;20 while ($str = trim(fgets($socket, 4096))) {21 $header .= $str;22 }23 24 $data = ;25 while (!feof($socket)) {26 $data .= fgets($socket, 4096);27 }28 29 return $data;30 }
curl版本:
1 /** 2 * curl版本 3 * 使用方法: 4 * $post_string = app=request&version=beta; 5 * request_by_curl('http://blog.snsgou.com/restserver.php', $post_string); 6 */ 7 function request_by_curl($remote_server, $post_string) { 8 $ch = curl_init(); 9 curl_setopt($ch, curlopt_url, $remote_server);10 curl_setopt($ch, curlopt_postfields, 'mypost=' . $post_string);11 curl_setopt($ch, curlopt_returntransfer, true);12 curl_setopt($ch, curlopt_useragent, snsgou.com's curl example beta);13 $data = curl_exec($ch);14 curl_close($ch);15 16 return $data;17 }
curl版本(2)
1 /** 2 * 发送http请求 3 * 4 * @param string $url 请求地址 5 * @param string $method 请求方式 get/post 6 * @param string $refererurl 请求来源地址 7 * @param array $data 发送数据 8 * @param string $contenttype 9 * @param string $timeout10 * @param string $proxy11 * @return boolean12 */13 function send_request($url, $data, $refererurl = '', $method = 'get', $contenttype = 'application/json', $timeout = 30, $proxy = false) {14 $ch = null;15 if('post' === strtoupper($method)) {16 $ch = curl_init($url);17 curl_setopt($ch, curlopt_post, 1);18 curl_setopt($ch, curlopt_header,0 );19 curl_setopt($ch, curlopt_fresh_connect, 1);20 curl_setopt($ch, curlopt_returntransfer, 1);21 curl_setopt($ch, curlopt_forbid_reuse, 1);22 curl_setopt($ch, curlopt_timeout, $timeout);23 if ($refererurl) {24 curl_setopt($ch, curlopt_referer, $refererurl);25 }26 if($contenttype) {27 curl_setopt($ch, curlopt_httpheader, array('content-type:'.$contenttype));28 }29 if(is_string($data)){30 curl_setopt($ch, curlopt_postfields, $data);31 } else {32 curl_setopt($ch, curlopt_postfields, http_build_query($data));33 }34 } else if('get' === strtoupper($method)) {35 if(is_string($data)) {36 $real_url = $url. (strpos($url, '?') === false ? '?' : ''). $data;37 } else {38 $real_url = $url. (strpos($url, '?') === false ? '?' : ''). http_build_query($data);39 }40 41 $ch = curl_init($real_url);42 curl_setopt($ch, curlopt_header, 0);43 curl_setopt($ch, curlopt_httpheader, array('content-type:'.$contenttype));44 curl_setopt($ch, curlopt_returntransfer, 1);45 curl_setopt($ch, curlopt_timeout, $timeout);46 if ($refererurl) {47 curl_setopt($ch, curlopt_referer, $refererurl);48 }49 } else {50 $args = func_get_args();51 return false;52 }53 54 if($proxy) {55 curl_setopt($ch, curlopt_proxy, $proxy);56 }57 $ret = curl_exec($ch);58 $info = curl_getinfo($ch);59 $contents = array(60 'httpinfo' => array(61 'send' => $data,62 'url' => $url,63 'ret' => $ret,64 'http' => $info,65 )66 );67 68 curl_close($ch);69 return $ret;70 }
调用 wcf接口 的一个例子:$json = restrequest($r_url,'post', json_encode($data));
