您的位置:

包含php实现post请求的词条

本文目录一览:

php实现模拟post请求用法实例

本文实例讲述了php实现模拟post请求的方法。分享给大家供大家参考。具体如下:

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;

希望本文所述对大家的php程序设计有所帮助。

php怎么以post方式发送数据

:用PHP向服务器发送HTTP的POST请求,代码如下:?php/***发送post请求*@paramstring$url请求地址*@paramarray$post_datapost键值对数据*@returnstring*/.

怎么用PHP发送POST请求

PHP发送POST请求的三种方式

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向服务器发送post请求

用PHP向服务器发送HTTP的POST请求,代码如下:

?php

/**    

 * 发送post请求    

 * @param string $url 请求地址    

 * @param array $post_data post键值对数据    

 * @return string    

 */    

function send_post($url, $post_data) {    

      $postdata = http_build_query($post_data);    

      $options = array(    

            'http' = array(    

                'method' = 'POST',    

                'header' = 'Content-type:application/x-www-form-urlencoded',    

                'content' = $postdata,    

                'timeout' = 15 * 60 // 超时时间(单位:s)    

            )    

        );    

        $context = stream_context_create($options);    

        $result = file_get_contents($url, false, $context);             

        return $result;    

}

使用的时候直接调用上面定义的send_post方法:

$post_data = array(

    'username' = 'username',

    'password' = 'password'

);

send_post('网址', $post_data);

PHP中怎样发送post请求并获取网页?

    $post='POST数据';

    // 初始化

    $curl = curl_init('URL');

    $header = array();

    $header[] = 'User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36';

    curl_setopt($curl, CURLOPT_HTTPHEADER, $header);

    // 不输出header头信息

    curl_setopt($curl, CURLOPT_HEADER, 0);

    // 保存到字符串而不是输出

    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

    // post数据

    curl_setopt($curl, CURLOPT_POST, 1);

    // 请求数据

    curl_setopt($curl, CURLOPT_POSTFIELDS, $post);

    // 是否抓取跳转后的页面

    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);

    $response = curl_exec($curl);

    curl_close($curl);

    echo $response;