一波PHP中cURL库的常见用法代码示例
程序员文章站
2024-04-02 11:18:10
php 的curl是不错的功能,下面收藏几段不错的片段
0、基本例子
一般流程:
$to_url=$_get['url'];
print_r($_get...
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);
上一篇: Laravel与CI框架中截取字符串函数
下一篇: php实现图片上传、剪切功能