php curl 使用集合

原文章轉自    it技術擎       裏面有更豐富的資料

下面的示例中舉例了php curl的幾種用法的彙總

包含
1、 post請求數據
2、get請求數據
3、發送特定格式的數據
4、在請求的時候帶cookie
下面是代碼:
function curl_post($url,$post_data = array(),$method="get", $cookie = array()){
        $session_id = md5($url).".log";   
        $ch = curl_init($url) ;  
        curl_setopt($ch, CURLOPT_HEADER,1);
        if(strtolower($method)=="post"){
            curl_setopt($ch, CURLOPT_POST,1);
            curl_setopt($ch, CURLOPT_POSTFIELDS,$post_data);
        }
        if(strtolower($method)=="json"){
            $post_data_str = json_encode($post_data);
            curl_setopt($ch, CURLOPT_POST,1);
            curl_setopt($ch, CURLOPT_POSTFIELDS,$post_data_str);
            curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                'Content-Type: application/json',
                'Content-Length: ' . strlen($post_data_str))
            );
        }
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,true); // 獲取數據返回  
        curl_setopt($ch, CURLOPT_BINARYTRANSFER,true); // 在啓用 CURLOPT_RETURNTRANSFER 時候將獲取數據返回  
        curl_setopt($ch, CURLOPT_COOKIEFILE,$session_id);
        curl_setopt($ch, CURLOPT_COOKIEJAR,$session_id);
        $output = curl_exec($ch) ;  
        curl_close($ch);
        return $output;
    }
$link = "http://it.techqing.com";
curl_post($link);
發佈了21 篇原創文章 · 獲贊 4 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章