<span id="mktg5"></span>

<i id="mktg5"><meter id="mktg5"></meter></i>

        <label id="mktg5"><meter id="mktg5"></meter></label>
        最新文章專題視頻專題問答1問答10問答100問答1000問答2000關鍵字專題1關鍵字專題50關鍵字專題500關鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關鍵字專題關鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
        問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
        當前位置: 首頁 - 科技 - 知識百科 - 正文

        一波PHP中cURL庫的常見用法代碼示例_php實例

        來源:懂視網 責編:小采 時間:2020-11-27 20:31:56
        文檔

        一波PHP中cURL庫的常見用法代碼示例_php實例

        一波PHP中cURL庫的常見用法代碼示例_php實例:php 的CURL是不錯的功能,下面收藏幾段不錯的片段 0、基本例子 一般流程: $to_url=$_GET['url']; print_r($_GET); if(substr($to_url,0,1)=='/'){ $to_url=http://www.amazon.com.$to_url; } echo $to_ur
        推薦度:
        導讀一波PHP中cURL庫的常見用法代碼示例_php實例:php 的CURL是不錯的功能,下面收藏幾段不錯的片段 0、基本例子 一般流程: $to_url=$_GET['url']; print_r($_GET); if(substr($to_url,0,1)=='/'){ $to_url=http://www.amazon.com.$to_url; } echo $to_ur

        php 的CURL是不錯的功能,下面收藏幾段不錯的片段

        0、基本例子
        一般流程:

        $to_url=$_GET['url'];
        print_r($_GET);
        if(substr($to_url,0,1)=='/'){
         $to_url="http://www.amazon.com".$to_url;
        }
        echo $to_url;
        //初始化
        $ch = curl_init();
        //設置選項,包括URL
        curl_setopt($ch, CURLOPT_URL, $to_url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        //執行并獲取HTML文檔內容
        $output = curl_exec($ch);
        $output=preg_replace("#href=\"#","href=\"http://in2.qq-ex.com/amazon.php?url=",$output);
        // 釋放curl句柄
        curl_close($ch);
        echo $output;
        // 指定代理地址
        curl_setopt($ch, CURLOPT_PROXY, '11.11.11.11:8080');
        // 如果需要的話,提供用戶名和密碼
        curl_setopt($ch, CURLOPT_PROXYUSERPWD,'user:pass');
        

        1、測試網站是否運行正常

         if (isDomainAvailible('http://gz.itownet.cn')) 
         { 
         echo "Up and running!"; 
         } 
         else 
         { 
         echo "Woops, nothing found there."; 
         } 
         
         //returns true, if domain is availible, false if not 
         function isDomainAvailible($domain) 
         { 
         //check, if a valid url is provided 
         if(!filter_var($domain, FILTER_VALIDATE_URL)) 
         { 
         return false; 
         } 
         
         //initialize curl 
         $curlInit = curl_init($domain); 
         curl_setopt($curlInit,CURLOPT_CONNECTTIMEOUT,10); 
         curl_setopt($curlInit,CURLOPT_HEADER,true); 
         curl_setopt($curlInit,CURLOPT_NOBODY,true); 
         curl_setopt($curlInit,CURLOPT_RETURNTRANSFER,true); 
         
         //get answer 
         $response = curl_exec($curlInit); 
         
         curl_close($curlInit); 
         
         if ($response) return true; 
         
         return false; 
         } 
        
        

        2、可以代替file_gecontents的操作

        function file_get_contents_curl($url) { 
         $ch = curl_init(); 
         
         curl_setopt($ch, CURLOPT_HEADER, 0); 
         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Set curl to return the data instead of printing it to the browser. 
         curl_setopt($ch, CURLOPT_URL, $url); 
         
         $data = curl_exec($ch); 
         curl_close($ch); 
         
         return $data; 
        } 
        
        

        3、保存某個網站下的所有圖片

         function getImages($html) { 
         $matches = array(); 
         $regex = '~http://somedomain.com/images/(.*?)\.jpg~i'; 
         preg_match_all($regex, $html, $matches); 
         foreach ($matches[1] as $img) { 
         saveImg($img); 
         } 
        } 
         
        function saveImg($name) { 
         $url = 'http://somedomain.com/images/'.$name.'.jpg'; 
         $data = get_data($url); 
         file_put_contents('photos/'.$name.'.jpg', $data); 
        } 
         
        $i = 1; 
        $l = 101; 
         
        while ($i < $l) { 
         $html = get_data('http://somedomain.com/id/'.$i.'/'); 
         getImages($html); 
         $i += 1; 
        } 
        
        

        4、FTP應用

        // open a file pointer 
        $file = fopen("/path/to/file", "r"); 
         
        // the url contains most of the info needed 
        $url = "ftp://username:password@mydomain.com:21/path/to/new/file"; 
         
        $ch = curl_init(); 
         
        curl_setopt($ch, CURLOPT_URL, $url); 
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
         
        // upload related options 
        curl_setopt($ch, CURLOPT_UPLOAD, 1); 
        curl_setopt($ch, CURLOPT_INFILE, $fp); 
        curl_setopt($ch, CURLOPT_INFILESIZE, filesize("/path/to/file")); 
         
        // set for ASCII mode (e.g. text files) 
        curl_setopt($ch, CURLOPT_FTPASCII, 1); 
         
        $output = curl_exec($ch); 
        curl_close($ch); 
        
        

        5、使用curl發送JSON數據

        $data = array("name" => "Hagrid", "age" => "36"); 
        $data_string = json_encode($data); 
         
        $ch = curl_init('http://api.local/rest/users'); 
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); 
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string); 
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
        curl_setopt($ch, CURLOPT_HTTPHEADER, array( 
         'Content-Type: application/json', 
         'Content-Length: ' . strlen($data_string)) 
        ); 
         
        $result = curl_exec($ch); 

        聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

        文檔

        一波PHP中cURL庫的常見用法代碼示例_php實例

        一波PHP中cURL庫的常見用法代碼示例_php實例:php 的CURL是不錯的功能,下面收藏幾段不錯的片段 0、基本例子 一般流程: $to_url=$_GET['url']; print_r($_GET); if(substr($to_url,0,1)=='/'){ $to_url=http://www.amazon.com.$to_url; } echo $to_ur
        推薦度:
        標簽: php curl php的curl
        • 熱門焦點

        最新推薦

        猜你喜歡

        熱門推薦

        專題
        Top
        主站蜘蛛池模板: 国产在线播放线91免费 | 一本色道久久综合亚洲精品| 亚洲国产成人手机在线观看| 在线观看免费人成视频色9 | 亚洲AV无码一区二区三区网址| 国外成人免费高清激情视频| 亚洲国产欧洲综合997久久| 四虎永久在线精品免费观看地址 | www一区二区www免费| 精品亚洲一区二区三区在线观看 | 亚洲精品私拍国产福利在线| 永久免费视频网站在线观看| 亚洲一区二区免费视频| 猫咪社区免费资源在线观看| 国产亚洲女在线线精品| 亚洲午夜未满十八勿入网站2| 在线观看免费无码专区| 亚洲综合视频在线观看| 毛片免费观看的视频在线| 国产精品亚洲综合网站| 国产成人亚洲综合无码精品 | 久久精品国产亚洲av麻豆小说| 国产成人精品免费午夜app| 学生妹亚洲一区二区| 亚洲AV无码乱码在线观看性色扶| 国产成人无码免费看片软件 | 亚洲AV天天做在线观看| 国产美女在线精品免费观看| 无码AV动漫精品一区二区免费 | 久久九九亚洲精品| 1000部禁片黄的免费看| 亚洲精华国产精华精华液网站| 亚洲精品麻豆av| 理论亚洲区美一区二区三区| 亚洲色精品88色婷婷七月丁香| 99久久免费看国产精品| 亚洲一区二区三区高清| 拔擦拔擦8x华人免费久久| 在线观看免费播放av片| 亚洲AV一区二区三区四区| 亚洲AV无码欧洲AV无码网站|