<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在线手机精品免费观看| 免费无毒a网站在线观看| 免费A级毛片无码专区| 久久久久国产亚洲AV麻豆| 色偷偷亚洲第一综合| 四虎在线播放免费永久视频 | 三年片在线观看免费观看大全一| 国产亚洲成人久久| a毛看片免费观看视频| 亚洲国产精彩中文乱码AV| 一级毛片不卡片免费观看| 亚洲一卡2卡三卡4卡有限公司| 亚洲免费精彩视频在线观看| 少妇中文字幕乱码亚洲影视| 国产大片免费网站不卡美女| 亚洲乱码在线观看| 亚洲精品无码日韩国产不卡?V| **真实毛片免费观看| 亚洲人成免费电影| 国产精品酒店视频免费看| 免费福利在线观看| 国产精一品亚洲二区在线播放 | 国产色无码精品视频免费| 伊人久久综在合线亚洲2019| 成人午夜免费福利| 9i9精品国产免费久久| 亚洲一区二区三区日本久久九| 国色精品卡一卡2卡3卡4卡免费| 亚洲av无码无线在线观看| 亚洲午夜国产精品无码老牛影视 | 玖玖在线免费视频| 亚洲国产精品一区二区久| 免费在线观看黄网站| 99久久国产精品免费一区二区| 亚洲小说图片视频| 亚洲精品国产电影| 精品无码免费专区毛片| 特色特黄a毛片高清免费观看| 久久精品国产亚洲AV无码麻豆| 国产一级做a爱免费视频| 91成人在线免费视频|