• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

php curl的几段小使用

php 搞代码 3年前 (2022-01-24) 14次浏览 已收录 0个评论

php curl的几段小应用

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

1 测试网站是否运行正常?
??

Java代码??

  1. <???
  2. ??
  3. ??if?(isDomainAvailible(http://gz.itownet.cn&#8217;))??
  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/&#8217;.$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/&#8217;.$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&#8221;;??
  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); ?

?


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:php curl的几段小使用
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址