一波PHP中cURL库的常见用法代码示例
php 的CURL是不错的功能,下面收藏几段不错的片段 0、基本例子一般流程:1、测试网站是否运行正常//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; }
} 2、可以代替file_gecontents的操作curl_setopt($ch,0);
curl_setopt($ch,1); //Set curl to return the data instead of printing it to the browser. curl_setopt($ch,$url); $data = curl_exec($ch); return $data; 3、保存某个网站下的所有图片function saveImg($name) {
$url = 'http://somedomain.com/images/'.$name.'.jpg'; $data = get_data($url); file_put_contents('photos/'.$name.'.jpg',$data); } $i = 1; while ($i < $l) { 4、FTP应用// 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,$url); // upload related options // set for ASCII mode (e.g. text files) $output = curl_exec($ch); 5、使用curl发送JSON数据 "Hagrid","age" => "36");
$data_string = json_encode($data);
$ch = curl_init('http://api.local/rest/users'); $result = curl_exec($ch); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |