包含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;

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/279464.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-20 15:04
下一篇 2024-12-20 15:04

相關推薦

  • PHP和Python哪個好找工作?

    PHP和Python都是非常流行的編程語言,它們被廣泛應用於不同領域的開發中。但是,在考慮擇業方向的時候,很多人都會有一個問題:PHP和Python哪個好找工作?這篇文章將從多個方…

    編程 2025-04-29
  • PHP怎麼接幣

    想要在自己的網站或應用中接受比特幣等加密貨幣的支付,就需要對該加密貨幣擁有一定的了解,並使用對應的API進行開發。本文將從多個方面詳細闡述如何使用PHP接受加密貨幣的支付。 一、環…

    編程 2025-04-29
  • 使用PHP foreach遍歷有相同屬性的值

    本篇文章將介紹如何使用PHP foreach遍歷具有相同屬性的值,並給出相應的代碼示例。 一、基礎概念 在講解如何使用PHP foreach遍歷有相同屬性的值之前,我們需要先了解幾…

    編程 2025-04-28
  • PHP獲取301跳轉後的地址

    本文將為大家介紹如何使用PHP獲取301跳轉後的地址。301重定向是什麼呢?當我們訪問一個網頁A,但是它已經被遷移到了另一個地址B,此時若服務器端做了301重定向,那麼你的瀏覽器在…

    編程 2025-04-27
  • PHP登錄頁面代碼實現

    本文將從多個方面詳細闡述如何使用PHP編寫一個簡單的登錄頁面。 1. PHP登錄頁面基本架構 在PHP登錄頁面中,需要包含HTML表單,用戶在表單中輸入賬號密碼等信息,提交表單後服…

    編程 2025-04-27
  • HTTP請求方式的選擇:POST還是GET?

    對於使用xxl-job進行任務調度的開發者,通常需要發送HTTP請求來執行一些任務。但是在發送請求時,我們總是會遇到一個問題:是使用POST還是GET?下面將從多個方面對這個問題進…

    編程 2025-04-27
  • 如何解決運行過程中的post-install問題

    一、post-install問題的定義 在編寫軟件程序時,通常需要進行一些額外的配置和設置,以確保軟件在其他系統中運行正常。其中一項設置是安裝軟件包,並在安裝後運行一個腳本來完成針…

    編程 2025-04-27
  • PHP與Python的比較

    本文將會對PHP與Python進行比較和對比分析,包括語法特性、優缺點等方面。幫助讀者更好地理解和使用這兩種語言。 一、語法特性 PHP語法特性: <?php // 簡單的P…

    編程 2025-04-27
  • 解決js ajax post 419問題

    對於使用ajax post請求時出現的419問題,我們需要進行以下幾個方面的闡述,包括返回碼的含義、可能出現的情況、解決方案等內容。 一、解析419返回碼 419返回碼錶示用戶超時…

    編程 2025-04-27
  • PHP版本管理工具phpenv詳解

    在PHP項目開發過程中,我們可能需要用到不同版本的PHP環境來試驗不同的功能或避免不同版本的兼容性問題。或者我們需要在同一台服務器上同時運行多個不同版本的PHP語言。但是每次手動安…

    編程 2025-04-24

發表回復

登錄後才能評論