一波PHP中cURL庫(kù)的常見(jiàn)用法代碼示例
php 的CURL是不錯(cuò)的功能,下面收藏幾段不錯(cuò)的片段
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();
//設(shè)置選項(xiàng),包括URL
curl_setopt($ch, CURLOPT_URL, $to_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
//執(zhí)行并獲取HTML文檔內(nèi)容
$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之curl設(shè)置超時(shí)實(shí)例
- php之curl實(shí)現(xiàn)http與https請(qǐng)求的方法
- PHP擴(kuò)展CURL的用法詳解
- PHP CURL獲取返回值的方法
- PHP中CURL的CURLOPT_POSTFIELDS參數(shù)使用細(xì)節(jié)
- php使用curl訪問(wèn)https示例分享
- PHP curl 獲取響應(yīng)的狀態(tài)碼的方法
- PHP CURL獲取cookies模擬登錄的方法
- PHP CURL CURLOPT參數(shù)說(shuō)明(curl_setopt)
- PHP中使用cURL實(shí)現(xiàn)Get和Post請(qǐng)求的方法
- php中使用Curl、socket、file_get_contents三種方法POST提交數(shù)據(jù)
- php運(yùn)行出現(xiàn)Call to undefined function curl_init()的解決方法
- 無(wú)法加載php_curl.dll解決辦法
- php的curl實(shí)現(xiàn)get和post的代碼
相關(guān)文章
thinkphp5 + ajax 使用formdata提交數(shù)據(jù)(包括文件上傳) 后臺(tái)返回json完整實(shí)例
這篇文章主要介紹了thinkphp5 + ajax 使用formdata提交數(shù)據(jù)(包括文件上傳) 后臺(tái)返回json操作,結(jié)合實(shí)例形式分析了thinkphp5 + ajax 使用formdata提交數(shù)據(jù)、文件上傳與后臺(tái)返回json遇到的相關(guān)問(wèn)題即解決方法,需要的朋友可以參考下2020-03-03
PHP array_key_exists檢查鍵名或索引是否存在于數(shù)組中的實(shí)現(xiàn)方法
下面小編就為大家?guī)?lái)一篇PHP array_key_exists檢查鍵名或索引是否存在于數(shù)組中的實(shí)現(xiàn)方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-06-06
ThinkPHP5.0框架使用build 自動(dòng)生成模塊操作示例
這篇文章主要介紹了ThinkPHP5.0框架使用build 自動(dòng)生成模塊操作,結(jié)合實(shí)例形式分析了thinkPHP5使用build自動(dòng)生成模塊的具體步驟、方法與相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2019-04-04
Drupal7連接多個(gè)數(shù)據(jù)庫(kù)及常見(jiàn)問(wèn)題解決
這篇文章主要介紹了Drupal7連接多個(gè)數(shù)據(jù)庫(kù)的方法、操作實(shí)例,以及常見(jiàn)問(wèn)題解決方法,需要的朋友可以參考下2014-03-03
詳解Laravel設(shè)置多態(tài)關(guān)系模型別名的方式
這篇文章主要介紹了Laravel 中簡(jiǎn)單設(shè)置多態(tài)關(guān)系模型別名的方式,需要的朋友可以參考下2019-10-10
ThinkPHP框架基于PDO方式連接數(shù)據(jù)庫(kù)操作示例
這篇文章主要介紹了ThinkPHP框架基于PDO方式連接數(shù)據(jù)庫(kù)操作,結(jié)合完整實(shí)例形式分析了thinkPHP使用PDO方式連接數(shù)據(jù)庫(kù)的相關(guān)配置、控制器及模板調(diào)用相關(guān)操作技巧,需要的朋友可以參考下2018-03-03

