首页 后端开发 php教程 PHP模拟POST的步骤

PHP模拟POST的步骤

Jun 13, 2016 pm 01:21 PM
curl quot

PHP模拟POST的方法

PHP主动POST数据到JSP

采用哪种方式好?

最好有例子,谢谢

------解决方案--------------------
后台模拟 哪种都差不多 最常用的curl模块
------解决方案--------------------

PHP code

function post_url($url, $post = "", $host = "www.ydtuiguang.com", $referrer = 'http://www.ydtuiguang.com/', $proxy = -1){
    if(function_exists("curl_init")){
        $ch = @curl_init();
        @curl_setopt($ch, CURLOPT_URL, $url);
        if(!empty($proxy["address"]))
            @curl_setopt($ch, CURLOPT_PROXY, strpos($proxy["address"], "http") === 0 ? $proxy["address"] : "http://".$proxy["address"]);
        if(!empty($proxy["account"]) && !empty($proxy["password"]))
            @curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxy["account"].":".$proxy["password"]);
        @curl_setopt($ch, CURLOPT_REFERER, $referrer);
        @curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.0)");
        @curl_setopt($ch, CURLOPT_COOKIEJAR, COOKIE_FILE_PATH);
        @curl_setopt($ch, CURLOPT_COOKIEFILE, COOKIE_FILE_PATH);
        @curl_setopt($ch, CURLOPT_HEADER, 0);
        @curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        @curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        @curl_setopt($ch, CURLOPT_TIMEOUT, 1000);

        if (!empty($post)) {
            @curl_setopt($ch, CURLOPT_POST, 1);
            @curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
        }

        $result = @curl_exec($ch);
        @curl_close($ch);
    }elseif(function_exists("fsockopen")){
        $httpheader = "POST ".$url." HTTP/1.1\r\n";   
        $httpheader .= "Accept: */*\r\n";   
        $httpheader .= "Accept-Language: zh-cn\r\n";   
        $httpheader .= "Referer: ".$referrer."\r\n";   
        $httpheader .= "Content-Type: application/x-www-form-urlencoded\r\n";   
        $httpheader .= "User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)\r\n";   
        $httpheader .= "Host: ".$host."\r\n";   
        $httpheader .= "Content-Length: ".strlen($post)."\r\n";   
        $httpheader .= "Connection: Keep-Alive\r\n";   
        $httpheader .= "\r\n";   
        $httpheader .= $post;   
        $fd = fsockopen($host, 80);   
        fwrite($fd, $httpheader);   
        $result = "";   
        while(!feof($fd)){   
            $result .= fread($fd, 8192);   
        }   
        fclose($fd);   
    }elseif(function_existes('file_get_contents')){
        $httpheader = "POST ".$url." HTTP/1.1\r\n";   
        $httpheader .= "Accept: */*\r\n";   
        $httpheader .= "Accept-Language: zh-cn\r\n";   
        $httpheader .= "Referer: ".$referrer."\r\n";   
        $httpheader .= "Content-Type: application/x-www-form-urlencoded\r\n";   
        $httpheader .= "User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)\r\n";   
        $httpheader .= "Host: ".$host."\r\n";   
        $httpheader .= "Content-Length: ".strlen($post)."\r\n";   
        $httpheader .= "Connection: Keep-Alive\r\n";   
        
        $opts = array(
           'http'=>array(
                'method'=>"POST",
                'header'=>$httpheader,
                'content'=>$post
           )
         );            
        $context = stream_context_create($opts);
        $result = file_get_contents($url, 'r', $context);
    }
    return $result;
}
<br><font color="#e78608">------解决方案--------------------</font><br>
登录后复制
探讨

后台模拟 哪种都差不多 最常用的curl模块
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热门文章

仓库:如何复兴队友
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.能量晶体解释及其做什么(黄色晶体)
1 周前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
3 周前 By 尊渡假赌尊渡假赌尊渡假赌

热门文章

仓库:如何复兴队友
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.能量晶体解释及其做什么(黄色晶体)
1 周前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
3 周前 By 尊渡假赌尊渡假赌尊渡假赌

热门文章标签

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

python中CURL和python requests的相互转换如何实现 python中CURL和python requests的相互转换如何实现 May 03, 2023 pm 12:49 PM

python中CURL和python requests的相互转换如何实现

Linux下更新curl版本教程! Linux下更新curl版本教程! Mar 07, 2024 am 08:30 AM

Linux下更新curl版本教程!

PHP8.1发布:引入curl多个请求并发处理 PHP8.1发布:引入curl多个请求并发处理 Jul 08, 2023 pm 09:13 PM

PHP8.1发布:引入curl多个请求并发处理

从头到尾:如何使用php扩展cURL进行HTTP请求 从头到尾:如何使用php扩展cURL进行HTTP请求 Jul 29, 2023 pm 05:07 PM

从头到尾:如何使用php扩展cURL进行HTTP请求

linux curl是什么 linux curl是什么 Apr 20, 2023 pm 05:05 PM

linux curl是什么

PHP Curl中如何处理网页的 301 重定向? PHP Curl中如何处理网页的 301 重定向? Mar 08, 2024 am 11:36 AM

PHP Curl中如何处理网页的 301 重定向?

php curl怎么设置cookie php curl怎么设置cookie Sep 26, 2021 am 09:27 AM

php curl怎么设置cookie

PHP Fatal error: Call to undefined function curl_setopt()的解决方法 PHP Fatal error: Call to undefined function curl_setopt()的解决方法 Jun 23, 2023 am 08:18 AM

PHP Fatal error: Call to undefined function curl_setopt()的解决方法

See all articles