php curl的几段小使用

WBOY
Release: 2016-06-13 11:00:42
Original
1061 people have browsed it

php curl的几段小应用

php 的CURL是不错的功能,下面收藏几段不错的片段?

1 测试网站是否运行正常?
??
Java代码??收藏代码
  1. ??
  2. ??
  3. ??if?(isDomainAvailible('http://gz.itownet.cn'))??
  4. ???????{??
  5. ???????????????echo?"Up?and?running!";??
  6. ???????}??
  7. ???????else??
  8. ???????{??
  9. ???????????????echo?"Woops,?nothing?found?there.";??
  10. ???????}??
  11. ??
  12. ???????//returns?true,?if?domain?is?availible,?false?if?not??
  13. ???????function?isDomainAvailible($domain)??
  14. ???????{??
  15. ???????????????//check,?if?a?valid?url?is?provided??
  16. ???????????????if(!filter_var($domain,?FILTER_VALIDATE_URL))??
  17. ???????????????{??
  18. ???????????????????????return?false;??
  19. ???????????????}??
  20. ??
  21. ???????????????//initialize?curl??
  22. ???????????????$curlInit?=?curl_init($domain);??
  23. ???????????????curl_setopt($curlInit,CURLOPT_CONNECTTIMEOUT,10);??
  24. ???????????????curl_setopt($curlInit,CURLOPT_HEADER,true);??
  25. ???????????????curl_setopt($curlInit,CURLOPT_NOBODY,true);??
  26. ???????????????curl_setopt($curlInit,CURLOPT_RETURNTRANSFER,true);??
  27. ??
  28. ???????????????//get?answer??
  29. ???????????????$response?=?curl_exec($curlInit);??
  30. ??
  31. ???????????????curl_close($curlInit);??
  32. ??
  33. ???????????????if?($response)?return?true;??
  34. ??
  35. ???????????????return?false;??
  36. ???????}??
  37. ?>??


2 可以代替file_gecontents的操作?
???
Java代码??收藏代码
  1. function?file_get_contents_curl($url)?{??
  2. ????$ch?=?curl_init();??
  3. ??
  4. ????curl_setopt($ch,?CURLOPT_HEADER,?0);??
  5. ????curl_setopt($ch,?CURLOPT_RETURNTRANSFER,?1);?//Set?curl?to?return?the?data?instead?of?printing?it?to?the?browser.??
  6. ????curl_setopt($ch,?CURLOPT_URL,?$url);??
  7. ??
  8. ????$data?=?curl_exec($ch);??
  9. ????curl_close($ch);??
  10. ??
  11. ????return?$data;??
  12. }??


3 保存某个网站下的所有图片?
??
Java代码??收藏代码
  1. ??function?getImages($html)?{??
  2. ????$matches?=?array();??
  3. ????$regex?=?'~http://somedomain.com/images/(.*?)\.jpg~i';??
  4. ????preg_match_all($regex,?$html,?$matches);??
  5. ????foreach?($matches[1]?as?$img)?{??
  6. ????????saveImg($img);??
  7. ????}??
  8. }??
  9. ??
  10. function?saveImg($name)?{??
  11. ????$url?=?'http://somedomain.com/images/'.$name.'.jpg';??
  12. ????$data?=?get_data($url);??
  13. ????file_put_contents('photos/'.$name.'.jpg',?$data);??
  14. }??
  15. ??
  16. $i?=?1;??
  17. $l?=?101;??
  18. ??
  19. while?($i?$l)?{??
  20. ????$html?=?get_data('http://somedomain.com/id/'.$i.'/');??
  21. ????getImages($html);??
  22. ????$i?+=?1;??
  23. }??


4 FTP应用?
?
Java代码??收藏代码
  1. //?open?a?file?pointer??
  2. $file?=?fopen("/path/to/file",?"r");??
  3. ??
  4. //?the?url?contains?most?of?the?info?needed??
  5. $url?=?"ftp://username:[email protected]:21/path/to/new/file";??
  6. ??
  7. $ch?=?curl_init();??
  8. ??
  9. curl_setopt($ch,?CURLOPT_URL,?$url);??
  10. curl_setopt($ch,?CURLOPT_RETURNTRANSFER,?1);??
  11. ??
  12. //?upload?related?options??
  13. curl_setopt($ch,?CURLOPT_UPLOAD,?1);??
  14. curl_setopt($ch,?CURLOPT_INFILE,?$fp);??
  15. curl_setopt($ch,?CURLOPT_INFILESIZE,?filesize("/path/to/file"));??
  16. ??
  17. //?set?for?ASCII?mode?(e.g.?text?files)??
  18. curl_setopt($ch,?CURLOPT_FTPASCII,?1);??
  19. ??
  20. $output?=?curl_exec($ch);??
  21. curl_close($ch); ?

?

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!