php curl的几段小使用

WBOY
풀어 주다: 2016-06-13 13:03:01
원래의
1066명이 탐색했습니다.

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:password@mydomain.com: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); ?

?

관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿