백엔드 개발 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으로 문의하세요.

뜨거운 기사 태그

메모장++7.3.1

메모장++7.3.1

사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전

SublimeText3 중국어 버전

중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기

스튜디오 13.0.1 보내기

강력한 PHP 통합 개발 환경

드림위버 CS6

드림위버 CS6

시각적 웹 개발 도구

SublimeText3 Mac 버전

SublimeText3 Mac 버전

신 수준의 코드 편집 소프트웨어(SublimeText3)

Python에서 CURL과 Python 요청 간의 상호 변환을 실현하는 방법 Python에서 CURL과 Python 요청 간의 상호 변환을 실현하는 방법 May 03, 2023 pm 12:49 PM

Python에서 CURL과 Python 요청 간의 상호 변환을 실현하는 방법

Linux에서 컬 버전을 업데이트하는 방법에 대한 튜토리얼입니다! Linux에서 컬 버전을 업데이트하는 방법에 대한 튜토리얼입니다! Mar 07, 2024 am 08:30 AM

Linux에서 컬 버전을 업데이트하는 방법에 대한 튜토리얼입니다!

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 요청을 만드는 방법

리눅스 컬이 뭐야? 리눅스 컬이 뭐야? Apr 20, 2023 pm 05:05 PM

리눅스 컬이 뭐야?

PHP Curl에서 웹 페이지의 301 리디렉션을 처리하는 방법은 무엇입니까? PHP Curl에서 웹 페이지의 301 리디렉션을 처리하는 방법은 무엇입니까? Mar 08, 2024 am 11:36 AM

PHP Curl에서 웹 페이지의 301 리디렉션을 처리하는 방법은 무엇입니까?

PHP 컬에서 쿠키를 설정하는 방법 PHP 컬에서 쿠키를 설정하는 방법 Sep 26, 2021 am 09:27 AM

PHP 컬에서 쿠키를 설정하는 방법

PHP 치명적인 오류에 대한 해결 방법: 정의되지 않은 함수 컬_setopt() 호출 PHP 치명적인 오류에 대한 해결 방법: 정의되지 않은 함수 컬_setopt() 호출 Jun 23, 2023 am 08:18 AM

PHP 치명적인 오류에 대한 해결 방법: 정의되지 않은 함수 컬_setopt() 호출

See all articles