php 模拟get提交

方法一:

$re = file_get_contents($url);
print_r($re);

方法二:

$ch = curl_init("http://www.jb51.net/") ; 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true) ; 
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true) ; 
$output = curl_exec($ch) ; 
$fh = fopen("out.html", 'w') ; 
fwrite($fh, $output) ; 
fclose($fh) ;

方法三:

function dfopen($url, $limit = 0, $post = '', $cookie = '', $bysocket = FALSE    , $ip = '', $timeout = 15, $block = TRUE, $encodetype  = 'URLENCODE') {
    $return = '';
    $matches = parse_url($url);
    $host = $matches['host'];
    $path = $matches['path'] ? $matches['path'].($matches['query'] ? '?'.$matches['query'] : '') : '/';
    $port = !empty($matches['port']) ? $matches['port'] : 80;

    if($post) {
        $out = "POST $path HTTP/1.0
";
        $out .= "Accept: */*
";
        $out .= "Accept-Language: zh-cn
";
        $boundary = $encodetype == 'URLENCODE' ? '' : ';'.substr($post, 0, trim(strpos($post, "
")));
        $out .= $encodetype == 'URLENCODE' ? "Content-Type: application/x-www-form-urlencoded
" : "Content-Type: multipart/form-data$boundary
";
        $out .= "User-Agent: $_SERVER[HTTP_USER_AGENT]
";                            
        $out .= "Host: $host
";
        $out .= 'Content-Length: '.strlen($post)."
";
        $out .= "Connection: Close
";
        $out .= "Cache-Control: no-cache
";
        $out .= "Cookie: $cookie

";
        $out .= $post;
    } else {
        $out = "GET $path HTTP/1.0
";
        $out .= "Accept: */*
";
        $out .= "Accept-Language: zh-cn
";
        $out .= "User-Agent: $_SERVER[HTTP_USER_AGENT]
";
        $out .= "Host: $host
";
        $out .= "Connection: Close
";
        $out .= "Cookie: $cookie

";
    }
    $fp = @fsockopen(($ip ? $ip : $host), $port, $errno, $errstr, $timeout);
    if(!$fp) {
        return '';
    } else {
        stream_set_blocking($fp, $block);
        stream_set_timeout($fp, $timeout);
        @fwrite($fp, $out);
        $status = stream_get_meta_data($fp);
        if(!$status['timed_out']) {
            while (!feof($fp)) {
                if(($header = @fgets($fp)) && ($header == "
" ||  $header == "
")){
                    break;
                }
            }
            $stop = false;
            while(!feof($fp) && !$stop) {
                $data = fread($fp, ($limit == 0 || $limit > 8192 ? 8192 : $limit));
                $return .= $data;
                if($limit) {
                    $limit -= strlen($data);
                    $stop = $limit <= 0;
                }
            }
        }
        @fclose($fp);
        return $return;
    }
}

 ================================================================下面是我用到的,可能和上面有重复,主要是https的问题

php中curl函数可以实现get与post操作,我们经常使用它来做一些人为模仿操作了,下面我来简单的介绍post与get的例子。
 

get 方法

 代码如下 复制代码
$url = "http://www.111cn.net /index.php?a=b&c=d&e=f&g=" . urlencode('王璐个人博客');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);    // 要求结果为字符串且输出到屏幕上
curl_setopt($ch, CURLOPT_HEADER, 0); // 不要http header 加快效率
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
$output = curl_exec($ch);
curl_close($ch);
var_dump($output);


post 方法

 代码如下 复制代码
$url = "http://www.111cn.net/ index.php";
$params = "a=b&c=d&e=f&g=" . urlencode('王璐个人博客');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);    // 要求结果为字符串且输出到屏幕上
curl_setopt($ch, CURLOPT_HEADER, 0); // 不要http header 加快效率
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
 
curl_setopt($ch, CURLOPT_POST, 1);    // post 提交方式
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
 
$output = curl_exec($ch);
curl_close($ch);
var_dump($output);

当请求https的数据时,会要求证书,这时候,加上下面这两个参数,规避ssl的证书检查

 代码如下 复制代码

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);    // https请求 不验证证书和hosts
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);

原文地址:https://www.cnblogs.com/zonglonglong/p/4303564.html