1. 程式人生 > 其它 >PHP curl方法集合

PHP curl方法集合

curl get請求

function curl_get($url, &$httpCode = 0)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

        //不做證書校驗,部署在linux環境下請改為true
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch
, CURLOPT_CONNECTTIMEOUT, 10); $file_contents = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); return $file_contents; }

curl post請求 傳送json資料

function post_json($url , $params = array()){
        //$data_string = json_encode($params,JSON_UNESCAPED_UNICODE);
$ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false
); curl_setopt($ch, CURLOPT_POSTFIELDS, $params); curl_setopt( $ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json' ) ); $data = curl_exec($ch); curl_close($ch); return ($data); }

curl post請求

function curl_post($url)
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_USERPWD, "username:password"); # 設定 API 帳號密碼
        curl_setopt($curl, CURLOPT_URL, $url);
        $ret = curl_exec($curl); # 獲取介面返回結果
        curl_close($curl);

        return $ret;
    }