国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

? ??? ?? PHP ???? PHP? ??? http ?? ??? ???

PHP? ??? http ?? ??? ???

Jul 25, 2016 am 08:58 AM

本文介紹一個(gè)php實(shí)現(xiàn)的http請(qǐng)求封裝類,封裝了三種post提交方法和一個(gè)request請(qǐng)求方法,并提供了調(diào)用示例,供大家學(xué)習(xí)參考。

在php編程調(diào)試時(shí),經(jīng)常需要模擬提交。 另外,在抓取一些頁面時(shí),需要經(jīng)常請(qǐng)求別人的頁面。

于是實(shí)現(xiàn)了一個(gè)http請(qǐng)求的封裝類,以方便調(diào)用。 本http請(qǐng)求類,封裝了三種post提交方法和一個(gè)request請(qǐng)求方法。

1,http請(qǐng)求封閉類

<?php
    /**
     *  HTTP常用請(qǐng)求封裝
     * @version $Id: HttpHelper.php,v 1.0 2012-8-9
     * @package library
     * @site bbs.it-home.org
     */

    // ---------------------------

    /**
     * http請(qǐng)求處理
     *
     * 開發(fā)中經(jīng)常需要模擬提交請(qǐng)求,本類封裝了常用的post方法
     *
     * @author ustb80
     *
     */
    class HttpHelper
    {
        // 當(dāng)前的user-agent字符串
        public $ua_string= "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:14.0) Gecko/20100101 Firefox/14.0.1";

        // 支持的提交方式
        public $post_type_list = array("curl", "socket", "stream");

        // 本地cookie文件
        private $cookie_file;

        // -----------------------

        /**
         * 構(gòu)造函數(shù)
         *
         * @param array $params 初始化參數(shù)
         */
        public function __construct($params = array())
        {
            if(count($params) > 0)
            {
                $this->init($params);
            }
        }

        // -----------------------

        /**
         * 參數(shù)初始化
         *
         * @param array $params
         */
        public function init($params)
        {
            if(count($params) > 0)
            {
                foreach($params as $key => $val)
                {
                    if(isset($this->$key))
                    {
                        $this->$key = $val;
                    }
                }
            }
        }

        // -----------------------

        /**
         * 提交請(qǐng)求
         *
         * @param string $url 請(qǐng)求地址
         * @param mixed $data 提交的數(shù)據(jù)
         * @param string $type 提交類型,curl,socket,stream可選
         */
        public function post($url, $data, $type = "socket")
        {
            if(!in_array($type, $this->post_type_list))
            {
                die("undefined post type");
            }
            $function_name = $type . "Post";
            return call_user_func_array(array($this, $function_name), array($url, $data));
        }

        // -----------------------

        /**
         * 更改默認(rèn)的ua信息
         *
         * 本方法常用于模擬各種瀏覽器
         *
         * @param string $ua_string UA字符串
         */
        public function setUA($user_agent)
        {
            $this->ua_string = $user_agent;
            return $this;
        }

        // -----------------------

        /**
         * 設(shè)置本地cookie文件
         *
         * 在用curl來模擬時(shí)常需要設(shè)置此項(xiàng)
         *
         * @param string $cookie_file 文件路徑
         */
        public function setCookieFile($cookie_file)
        {
            $this->cookie_file = $cookie_file;
            return $this;
        }

        // -----------------------

        /**
         * curl方式提交
         *
         * @param string $url 請(qǐng)求地址
         * @param mixed $data 提交的數(shù)據(jù)
         * @param string $user_agent 自定義的UA
         * @return mixed
         */
        public function curlPost($url, $data, $user_agent = '')
        {
            if($user_agent == '')
            {
                $user_agent = $this->ua_string;
            }

            if (!is_array($data))
            {
                $data = array($data);
            }

            $data = http_build_query($data);

            if (!function_exists("curl_init"))
            {
                die('undefined function curl_init');
            }

            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
            $rs = curl_exec($ch);
            curl_close($ch);
            return $rs;
        }

        // -----------------------

        /**
         * 套接字提交
         *
         * @param string $url 請(qǐng)求地址
         * @param mixed $data 提交的數(shù)據(jù)
         * @param string $user_agent 自定義的UA
         * @param int $port 端口
         * @param int $timeout 超時(shí)限制
         * @return mixed
         */
        public function socketPost($url, $data, $user_agent = '', $port = 80, $timeout = 30)
        {
            $url_info = parse_url($url);
            $remote_server = $url_info['host'];
            $remote_path = $url_info['path'];
            $socket = fsockopen($remote_server, $port, $errno, $errstr, $timeout);
            if(!$socket)
            {
                die("$errstr($errno)");
            }

            if($user_agent == '')
            {
                $user_agent = $this->ua_string;
            }

            if (!is_array($data))
            {
                $data = array($data);
            }

            $data = http_build_query($data);

            fwrite($socket, "POST {$remote_path} HTTP/1.0\r\n");
            fwrite($socket, "User-Agent: {$user_agent}\r\n");
            fwrite($socket, "HOST: {$remote_server}\r\n");
            fwrite($socket, "Content-type: application/x-www-form-urlencoded\r\n");
            fwrite($socket, "Content-length: " . strlen($data) . "\r\n");
            fwrite($socket, "Accept:*/*\r\n");
            fwrite($socket, "\r\n");
            fwrite($socket, "{$data}\r\n");
            fwrite($socket, "\r\n");

            $header = "";
            while($str = trim(fgets($socket, 4096)))
            {
                $header .= $str;
            }

            $data = "";
            while(!feof($socket))
            {
                $data .= fgets($socket, 4096);
            }

            return $data;
        }

        // -----------------------

        /**
         * 文件流提交
         *
         * @param string $url 提交地址
         * @param string $data 數(shù)據(jù)
         * @param string $user_agent 自定義的UA
         * @return mixed
         */
        public function streamPost($url, $data, $user_agent = '')
        {
            if($user_agent == '')
            {
                $user_agent = $this->ua_string;
            }

            if (!is_array($data))
            {
                $data = array($data);
            }

            $data = http_build_query($data);
            $context = array(
                    'http' => array(
                            'method' => 'POST',
                            'header' => 'Content-type: application/x-www-form-urlencoded'
 . "\r\n" . 'User-Agent : ' . $user_agent . "\r\n" . 'Content-length: ' . strlen($data),
                            'content' => $data
                    )
            );
            $stream_context = stream_context_create($context);
            $data = file_get_contents($url, FALSE, $stream_context);
            return $data;
        }

        // -----------------------

        /**
         * 發(fā)送請(qǐng)求
         *
         * 本方法通過curl函數(shù)向目標(biāo)服務(wù)器發(fā)送請(qǐng)求
         *
         * @param string $url 請(qǐng)求地址
         * @return mixed
         */
        public function request($url)
        {
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_USERAGENT, !empty($this->ua_string)? $this->ua_string : $_SERVER['HTTP_USER_AGENT']);
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

            if (isset($this->cookie_file))
            {
                curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookie_file);
            }
            $data = curl_exec($ch);
            curl_close($ch);

            return $data;
        }
    }
?>

調(diào)用示例:

<?php
    require_once 'HttpHelper.php';
    $HttpHelper = new HttpHelper();

    $url = "http://localhost/post.php";

    $data = array("name"=>"socket");
    $rs[] = $HttpHelper->post($url, $data);

    $data = array("name"=>"curl");
    $rs[] = $HttpHelper->post($url, $data, "curl");

    $data = array("name"=>"stream");
    $rs[] = $HttpHelper->post($url, $data, "stream");

    $rs[] = $HttpHelper->request($url);

    print_r($rs);
?>

3,post.php文件:

<?php
    echo 'test request:';
    print_r($_REQUEST);
?>

4,輸出結(jié)果:

Array ( [0] => test request:Array ( [name] => socket )

[1] => test request:Array ( [name] => curl )

[2] => test request:Array ( [name] => stream )

[3] => test request:Array ( )

)



? ????? ??
? ?? ??? ????? ???? ??? ??????, ???? ?????? ????. ? ???? ?? ???? ?? ??? ?? ????. ???? ??? ???? ???? ??? ?? admin@php.cn?? ?????.

? AI ??

Undresser.AI Undress

Undresser.AI Undress

???? ?? ??? ??? ?? AI ?? ?

AI Clothes Remover

AI Clothes Remover

???? ?? ???? ??? AI ?????.

Video Face Swap

Video Face Swap

??? ??? AI ?? ?? ??? ???? ?? ???? ??? ?? ????!

???

??? ??

???++7.3.1

???++7.3.1

???? ?? ?? ?? ???

SublimeText3 ??? ??

SublimeText3 ??? ??

??? ??, ???? ?? ????.

???? 13.0.1 ???

???? 13.0.1 ???

??? PHP ?? ?? ??

???? CS6

???? CS6

??? ? ?? ??

SublimeText3 Mac ??

SublimeText3 Mac ??

? ??? ?? ?? ?????(SublimeText3)

???

??? ??

?? ????
1742
16
Cakephp ????
1596
56
??? ????
1536
28
PHP ????
1396
31
???
PHP ?? API ????? ?? ??? ?????? PHP ?? API ????? ?? ??? ?????? Jun 14, 2025 am 12:27 AM

ToversionAphp ??, forclarityandeasofrouting, ac

PHP?? ?? ? ??? ????? ????????? PHP?? ?? ? ??? ????? ????????? Jun 20, 2025 am 01:03 AM

TOSECURELYHANDLEAUSTENCENDACTIONANDACTERIZINGINPHP, FORCUCTSESTEPS : 1. ALWAYSHASHPASSWORTHPASSWORD_HASH () ? VERVERIFYUSINGPANSWORD_VERIFY (), usePREPAREDSTATEMENTSTOPREVENTSQLINGERGED, andSTOREUSERSESSEATAIN $ _SESSIONSAFTERLOGIN.2.impleplempletrole ?? ACCESSC

PHP? ?? ? ?? ?? ????? ????? ???? ?????? PHP? ?? ? ?? ?? ????? ????? ???? ?????? Jun 14, 2025 am 12:25 AM

ProceduralAndObject-OrientedProgramming (OOP) InphpDiffersiMINTIFINTIONTERINGLISTURE, ??? ? ? DATAHANDLING

PHP? ?? ?? (??)? ???? ?? ?? ? ? ????? PHP? ?? ?? (??)? ???? ?? ?? ? ? ????? Jun 14, 2025 am 12:25 AM

phpdoesnothaveAbuilt-inweakMapButofferSweakReference.1.WeakReenceAllowsholdingReferences withoutpreventinggarbageCollection.2.ItusteForCaching, Eventlisteners, andMetAdataWithoutAftingObjectLifeCycles.3.youcoucococococococcinccing

PHP?? ?? ???? ??? ??? ?? ? ? ??????? PHP?? ?? ???? ??? ??? ?? ? ? ??????? Jun 19, 2025 am 01:05 AM

PHP?? ?? ???? ???? ????? ??? ?? ??? ???? ?? ??? ??? ??? ???? ????. 1. finfo_file ()? ???? ?? ?? ??? ???? ???/jpeg? ?? ?? ?? ? ?????. 2. uniqid ()? ???? ??? ?? ??? ???? ? Web ?? ????? ??????. 3. php.ini ? html ??? ?? ?? ??? ???? ???? ??? 0755? ?????. 4. Clamav? ???? ???? ???? ??? ??????. ??? ??? ?? ???? ????? ???? ?? ??? ????? ???? ??? ? ??? ?????.

PHP?? == (??? ??)? === (??? ??)? ???? ?????? PHP?? == (??? ??)? === (??? ??)? ???? ?????? Jun 19, 2025 am 01:07 AM

PHP?? ==? ==? ?? ???? ?? ??? ??????. == ?? ??? ?? ?? ?????. ?? ??, 5 == "5"? true? ????, ?? ??? ???? ?? ?? ??? ????? ????? (? : 5 === "5"? false? ?????. ?? ?????? ===? ? ???? ?? ?????? == ?? ??? ??? ???? ?????.

PHP? NOSQL ?????? (? : MongoDB, Redis)? ??? ?? ??? ? ????? PHP? NOSQL ?????? (? : MongoDB, Redis)? ??? ?? ??? ? ????? Jun 19, 2025 am 01:07 AM

?, PHP? ?? ?? ?? ?????? ?? MongoDB ? Redis? ?? NOSQL ??????? ?? ??? ? ????. ?? MongoDBPHP ???? (PECL ?? Composer? ?? ??)? ???? ????? ????? ??? ?????? ? ???? ????? ??, ??, ?? ? ?? ??? ?????. ??, Predis ????? ?? Phpredis ??? ???? Redis? ???? ?? ? ?? ? ??? ???? ??? ????? Phpredis? ???? ?? Predis? ?? ??? ?????. ? ? ?? ??? ???? ? ????? ????.

php (, -, *, /, %)?? ?? ??? ??? ?????? php (, -, *, /, %)?? ?? ??? ??? ?????? Jun 19, 2025 pm 05:13 PM

PHP?? ?? ??? ??? ???? ??? ??? ????. 1. ?? ??? ?? ? ?? ??? ??? ???? ???? ??? ? ????. ??? ??? ???? ????? ????? ???? ????. 2. ?? ?? ?? - ??, ??? ???? ?? ??? ?????. 3. ?? ???? ??? ??? ???? ??? ??? ?????. 4. Division? / ??? ???? 0?? ??? ?? ????? ??? ?? ??? ?? ? ? ????. 5. ???? ??? ???? ?? ?? ? ?? ??? ???? ? ??? ? ???, ??? ?? ? ? ??? ??? ???? ?????. ? ???? ???? ???? ??? ??? ??? ???? ?? ??? ? ??????? ????.

See all articles