PHP中使用cURL實現Get和Post請求的方法

來源:互聯網
上載者:User

標籤:執行   介紹   使用說明   變數   function   manual   als   方法   header   

cURL是PHP的工具類,使用時參考官方文檔:http://php.net/manual/zh/book.curl.php

裡面有詳細的使用說明以及參數介紹。

    /**     * @param string $url     * @return mixed     */    public function doGet($url)    {        //初始化        $ch = curl_init();        curl_setopt($ch, CURLOPT_URL,$url);        // 執行後不直接列印出來        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);        curl_setopt($ch, CURLOPT_HEADER, false);        // 跳過認證檢查        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);        // 不從認證中檢查SSL密碼編譯演算法是否存在        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);        //執行並擷取HTML文檔內容        $output = curl_exec($ch);        //釋放curl控制代碼        curl_close($ch);                return $output;    }    /**     * @param string $url     * @param array $post_data     * @param array | boolean $header     * @return mixed     */    public function doPost($url,$post_data,$header)    {        $ch = curl_init();        curl_setopt($ch, CURLOPT_URL, $url);        // 執行後不直接列印出來        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);        // 佈建要求方式為post        curl_setopt($ch, CURLOPT_POST, true);        // post的變數        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);        // 要求標頭,可以傳數組        curl_setopt($ch, CURLOPT_HEADER, $header);        // 跳過認證檢查        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);        // 不從認證中檢查SSL密碼編譯演算法是否存在        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);        $output = curl_exec($ch);        curl_close($ch);        return $output;    }

其中跳過認證是為了訪問https。

PHP中使用cURL實現Get和Post請求的方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.