- //Curl Get数据
- function _curl_get($url) {
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL,$url);
- curl_setopt($ch, CURLOPT_HEADER, 0);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- $data = curl_exec($ch);
- curl_close($ch);
- if ($data)
- return $data;
- else
- return false;
- }
- //Curl Post数据
- function _curl_post($url, $vars) {
- $ch = curl_init();
- curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_URL,$url);
- curl_setopt($ch, CURLOPT_POST, 1 );
- curl_setopt($ch, CURLOPT_POSTFIELDS, $vars);
- $data = curl_exec($ch);
- curl_close($ch);
- if ($data)
- return $data;
- else
- return false;
- }
通用函数
- /**
- * 使用GET方法发送HTTP请求
- *
- * @param string $url 需要请求的URL,完整URL,例如:http://www.example.com:8080/test.php?parm1=var1&parm2=var2
- * @param array/string $cookies 如果有COOKIE数据可以发送过去,可以是Cookie数组,也可以是Cookie字符串
- * @return mixed 成功返回GET回来的数据,失败返回false
- */
- function http_get($url, $cookies = array()) {
- /**
- * 使用cURL处理GET请求
- */
- if (function_exists('curl_init')){
- //组织COOKIE数据
- $header = array();
- if (!emptyempty($cookies)){
- if (is_array($cookies)){
- $encoded = '';
- while (list($k,$v) = each($cookies)) {
- $encoded .= ($encoded ? ";" : "");
- $encoded .= rawurlencode($k)."=".rawurlencode($v);
- }
- $header = array("Cookie :". $encoded);
- } elseif (is_string($cookies)){
- if (strtolower(substr($cookies, 0, 7)) == 'cookie:'){
- $header = array($cookies);
- } else {
- $header = array("Cookie: ". $cookies);
- }
- }
- }
- //处理请求
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL,$url);
- curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
- curl_setopt($ch, CURLOPT_HEADER, 0);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- $data = curl_exec($ch);
- curl_close($ch);
- if ($data)
- return $data;
- else
- return false;
- }
- /**
- * 使用fsockopen处理GET请求
- */
- else {
- //组织COOKIE数据
- $cookie = '';
- if (!emptyempty($cookies)){
- if (is_array($cookies)){
- $encoded = '';
- while (list($k,$v) = each($cookies)) {
- $encoded .= ($encoded ? ";" : "");
- $encoded .= rawurlencode($k)."=".rawurlencode($v);
- }
- $cookie = $encoded;
- } elseif (is_string($cookies)){
- if (strtolower(substr($cookies, 0, 7)) == 'cookie:'){
- $cookie = substr($cookies, 7);
- } else {
- $cookie = $cookies;
- }
- }
- }
- //处理请求
- $url = parse_url($url);
- if (strtolower($url['scheme']) != 'http' && $url['scheme'] != ''){
- return false;
- }
- if ( !($fp = fsockopen($url['host'], $url['port'] ? $url['port'] : 80, $errno, $errstr, 10))){
- return false;
- }
- fputs($fp, sprintf("GET %s%s%s HTTP/1.0/n", $url['path'], $url['query'] ? "?" : "", $url['query']));
- fputs($fp, "Host: $url[host]/n");
- fputs($fp, "User-Agent: HFHttp-Client/n");
- if ($cookie != ''){
- fputs($fp, "Cookie: $cookie/n/n");
- }
- fputs($fp, "Connection: close/n/n");
- fputs($fp, "$post /n");
- $ret = '';
- while (!feof($fp)) {
- $c = fgets($fp, 1024);
- if (preg_match("/^HTTP/", $c) || preg_match("/^P3P/", $c) || preg_match("/^([A-Za-z/-]+)/:/s*(.*)$/", $c)){
- continue;
- }
- if (trim($c) == ""){
- continue;
- }
- $ret .= $c;
- }
- fclose($fp);
- return $ret;
- }
- }
- /**
- * 使用POST方法发送HTTP请求
- *
- * @param string $url 需要请求的URL,完整URL,例如:http://www.example.com:8080/test.php?parm1=var1&parm2=var2
- * @param array $vars 需要POST提交的变量数组
- * @param array/string $cookies 如果有COOKIE数据可以发送过去,可以是Cookie数组,也可以是Cookie字符串
- * @return mixed 成功返回GET回来的数据,失败返回false
- */
- function http_post($url, $vars = array(), $cookies = array()) {
- /**
- * 使用cURL处理POST请求
- */
- if (function_exists('curl_init')){
- //组织COOKIE数据
- $header = array();
- if (!emptyempty($cookies)){
- if (is_array($cookies)){
- $encoded = '';
- while (list($k,$v) = each($cookies)) {
- $encoded .= ($encoded ? ";" : "");
- $encoded .= rawurlencode($k)."=".rawurlencode($v);
- }
- $header = array("Cookie :". $encoded);
- } elseif (is_string($cookies)){
- if (strtolower(substr($cookies, 0, 7)) == 'cookie:'){
- $header = array($cookies);
- } else {
- $header = array("Cookie: ". $cookies);
- }
- }
- }
- //执行POST请求
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL,$url);
- curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
- curl_setopt($ch, CURLOPT_POST, 1 );
- curl_setopt($ch, CURLOPT_POSTFIELDS, $vars);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- $data = curl_exec($ch);
- curl_close($ch);
- if ($data)
- return $data;
- else
- return false;
- }
- /**
- * 使用fsockopen处理POST请求
- */
- else {
- //组织COOKIE数据
- $cookie = '';
- if (!emptyempty($cookies)){
- if (is_array($cookies)){
- $encoded = '';
- while (list($k,$v) = each($cookies)) {
- $encoded .= ($encoded ? ";" : "");
- $encoded .= rawurlencode($k)."=".rawurlencode($v);
- }
- $cookie = $encoded;
- } elseif (is_string($cookies)){
- if (strtolower(substr($cookies, 0, 7)) == 'cookie:'){
- $cookie = substr($cookies, 7);
- } else {
- $cookie = $cookies;
- }
- }
- }
- //组织POST数据
- $post = '';
- if (!emptyempty($vars)){
- if (is_array($vars)){
- $encoded = '';
- while (list($k,$v) = each($vars)) {
- $encoded .= ($encoded ? "&" : "");
- $encoded .= rawurlencode($k)."=".rawurlencode($v);
- }
- $post = $encoded;
- } else {
- $post = $vars;
- }
- }
- //处理请求
- $url = parse_url($url);
- if (strtolower($url['scheme']) != 'http' && $url['scheme'] != ''){
- return false;
- }
- if ( !($fp = fsockopen($url['host'], $url['port'] ? $url['port'] : 80, $errno, $errstr, 10))){
- return false;
- }
- fputs($fp, sprintf("POST %s%s%s HTTP/1.0/n", $url['path'], $url['query'] ? "?" : "", $url['query']));
- fputs($fp, "Host: $url[host]/n");
- fputs($fp, "User-Agent: HFHttp-Client/n");
- if ($cookie != ''){
- fputs($fp, "Cookie: $cookie/n/n");
- }
- fputs($fp, "Content-type: application/x-www-form-urlencoded/n");
- fputs($fp, "Content-length: " . strlen($post) . "/n");
- fputs($fp, "Connection: close/n/n");
- fputs($fp, "$post /n");
- $ret = '';
- while (!feof($fp)) {
- $c = fgets($fp, 1024);
- if (preg_match("/^HTTP/", $c) || preg_match("/^P3P/", $c) || preg_match("/^([A-Za-z/-]+)/:/s*(.*)$/", $c)){
- continue;
- }
- if (trim($c) == ""){
- continue;
- }
- $ret .= $c;
- }
- fclose($fp);
- return $ret;
- }
- }