> php教程 > PHP源码 > 본문

PHP发送POST请求的三种方式

PHP中文网
풀어 주다: 2016-05-25 17:09:10
원래의
1341명이 탐색했습니다.

PHP代码

class Request{
 
    public static function post($url, $post_data = '', $timeout = 5){//curl
 
        $ch = curl_init();
 
        curl_setopt ($ch, CURLOPT_URL, $url);
 
        curl_setopt ($ch, CURLOPT_POST, 1);
 
        if($post_data != ''){
 
            curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
 
        }
 
        curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 
 
        curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
 
        curl_setopt($ch, CURLOPT_HEADER, false);
 
        $file_contents = curl_exec($ch);
 
        curl_close($ch);
 
        return $file_contents;
 
    }
 
 
    public static function post2($url, $data){//file_get_content
 
         
 
        $postdata = http_build_query(
 
            $data
 
        );
 
         
 
        $opts = array('http' =>
 
                      array(
 
                          'method'  => 'POST',
 
                          'header'  => 'Content-type: application/x-www-form-urlencoded',
 
                          'content' => $postdata
 
                      )
 
        );
 
         
 
        $context = stream_context_create($opts);
 
 
        $result = file_get_contents($url, false, $context);
 
        return $result;
 
 
    }
 
 
    public static function post3($host,$path,$query,$others=''){//fsocket
 
 
        $post="POST $path HTTP/1.1\r\nHost: $host\r\n";
 
        $post.="Content-type: application/x-www-form-";
 
        $post.="urlencoded\r\n${others}";
 
        $post.="User-Agent: Mozilla 4.0\r\nContent-length: ";
 
        $post.=strlen($query)."\r\nConnection: close\r\n\r\n$query";
 
        $h=fsockopen($host,80);
 
        fwrite($h,$post);
 
        for($a=0,$r='';!$a;){
 
                $b=fread($h,8192);
 
                $r.=$b;
 
                $a=(($b=='')?1:0);
 
            }
 
        fclose($h);
 
        return $r;
 
    }
}
로그인 후 복사

                   

관련 라벨:
php
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
최신 이슈
인기 추천
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!