### file_get_content POST ```php array( 'method' => 'POST', 'header' => 'Content-type:application/x-www-form-urlencoded', 'content' => $postdata, 'timeout' => 15 * 60 // 超时时间(单位:s) ) ); $context = stream_context_create($options); $result = file_get_contents($url, false, $context); return $result; } 使用如下: $post_data = array('username' => 'stclair2201','password' => 'handan'); send_post('http://blog.snsgou.com', $post_data); ``` 注:使用上述代码给另一台服务器发送http请求时,发现服务器处理请求时间过长,本地的PHP就会中断请求,即所谓的超时中断。原因是http请求本身的时间限制,遂查看PHP手册,发现 “timeout” 参数,设置长一些的超时时间。 ### file_get_content GET ```php ``` ### 用fopen打开url, 以get方式获取内容 ```php ``` ### 方法4:用fsockopen函数打开url,以get方式获取完整的数据,包括header和body,fsockopen需要 PHP.ini 中 allow_url_fopen 选项开启 ```php ``` ### 方法5:用fsockopen函数打开url,以POST方式获取完整的数据,包括header和body ```php $value) $values[]="$key=".urlencode($value); $data_string=implode("&",$values); // Find out which port is needed - if not given use standard (=80) if(!isset($URL_Info["port"])) $URL_Info["port"]=80; // building POST-request: $request.="POST ".$URL_Info["path"]." HTTP/1.1\n"; $request.="Host: ".$URL_Info["host"]."\n"; $request.="Referer: $referer\n"; $request.="Content-type: application/x-www-form-urlencoded\n"; $request.="Content-length: ".strlen($data_string)."\n"; $request.="Connection: close\n"; $request.="Cookie: $cookie\n"; $request.="\n"; $request.=$data_string."\n"; $fp = fsockopen($URL_Info["host"],$URL_Info["port"]); fputs($fp, $request); while(!feof($fp)) { $result .= fgets($fp, 1024); } fclose($fp); return $result; } ?> ``` ### Socket版本 ```php /** * Socket版本 * 使用方法: * $post_string = "app=socket&version=beta"; * request_by_socket('blog.snsgou.com', '/restServer.php', $post_string); */ 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; } ``` ### CURL ```php /** * Curl版本 * 使用方法: * $post_string = "app=request&version=beta"; * request_by_curl('http://blog.snsgou.com/restServer.php', $post_string); */ function request_by_curl($remote_server, $post_string) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $remote_server); curl_setopt($ch, CURLOPT_POSTFIELDS, 'mypost=' . $post_string); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_USERAGENT, "snsgou.com's CURL Example beta"); $data = curl_exec($ch); curl_close($ch); return $data; } ``` ### CURL ```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 * @return boolean */ 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 $ret; } ``` ### CURL post-get ```php public function httpsSend($url, $params = array(), $method = 'get') { $ch = curl_init(); if($method == 'get') { $sParm = http_build_query($params); if(!empty($sParm)) { $url = $url.'?'.$sParm; } } else { curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params)); } curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_TIMEOUT, 500); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); try { $response = curl_exec($ch); if(!$response) { $response = json_encode(array( 'errcode'=>curl_errno($ch), 'errmsg'=>curl_error($ch), )); } curl_close($ch); } catch (Exception $e) { $response = json_encode(array()); } return json_decode($response, true); } ```