1. 程式人生 > >CURL請求GET與POST

CURL請求GET與POST

<?php


class Action
{
    public static function curl_get($url){

           $testurl = $url;
           $ch = curl_init();  
           curl_setopt($ch, CURLOPT_URL, $testurl);  
            //引數為1表示資料傳輸。為0表示直接輸出顯示。
           curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            //引數為0表示不帶標頭檔案,為1表示帶標頭檔案
           curl_setopt($ch, CURLOPT_HEADER,0);
           $output = curl_exec($ch); 
           curl_close($ch); 
           return $output;
     }
    /*
     * url:訪問路徑
     * array:要傳遞的陣列
     * */
    public static function curl_post($url,$array){

        $curl = curl_init();
        //設定提交的url
        curl_setopt($curl, CURLOPT_URL, $url);
        //設定標頭檔案的資訊作為資料流輸出
        curl_setopt($curl, CURLOPT_HEADER, 0);
        //設定獲取的資訊以檔案流的形式返回。而不是直接輸出。
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        //設定post方式提交
        curl_setopt($curl, CURLOPT_POST, 1);
        //設定post資料
        $post_data = $array;
        curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
        //執行命令
        $data = curl_exec($curl);
        //關閉URL請求
        curl_close($curl);
      //獲得資料並返回
        return $data;
    }
}