国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > PHP > 正文

一波PHP中cURL庫(kù)的常見用法代碼示例

2020-03-22 19:07:10
字體:
供稿:網(wǎng)友
$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、測(cè)試網(wǎng)站是否運(yùn)行正常 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、保存某個(gè)網(wǎng)站下的所有圖片 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應(yīng)用 // 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發(fā)送JSON數(shù)據(jù)
$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); PHP教程

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 永德县| 黎城县| 阳东县| 安阳县| 二连浩特市| 大理市| 扶绥县| 孟州市| 广平县| 常山县| 香港| 凤山县| 开化县| 波密县| 文安县| 白河县| 太保市| 三原县| 双峰县| 青州市| 饶阳县| 老河口市| 浦县| 临西县| 安福县| 恭城| 龙川县| 汶上县| 岳池县| 马关县| 临夏市| 鄂尔多斯市| 富锦市| 南陵县| 辽源市| 互助| 舞阳县| 会同县| 灵寿县| 滦平县| 德化县|