php 模拟post请求

开发技术 作者: 2024-07-27 16:35:01
php 模拟post请求

下面是编程之家 jb51.cc 通过网络收集整理的代码片段。

编程之家小编现在分享给大家,也给大家做个参考。

 
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=array()){//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;
   
    }
}
$url='http://******/con/Inter.PHP';
$data=Request::post($url,array('api'=>'tag_list'));
$data2=Request::post2($url,array('api'=>'tag_list'));
echo $data;
 

以上是编程之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。

如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。

原创声明
本站部分文章基于互联网的整理,我们会把真正“有用/优质”的文章整理提供给各位开发者。本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
本文链接:http://www.jiecseo.com/news/show_51786.html
php 模拟post请求