PHP Curl Simple capture Image generation base64 code (with curl function parameter description)

Source: Internet
Author: User
Tags base64 vars

    1. Many years ago the code suddenly took a use, special to do this note (here code to locate the internal error location, non-HTTP code)
[PHP]View PlainCopy
  1. <?php
  2. $url ="http://c.hiphotos.baidu.com/image/w%3D210/sign=ed30880babec8a13141a50e1c7029157/  D52a2834349b033be1a9503e17ce36d3d539bd35.gif ";
  3. function Curl_url ($url,$type =0,$timeout =30) {
  4. $msg = [' code ' =>2100,' status ' = 'error ',' msg ' = ' =' unknown error!  ‘];
  5. $imgs = [' image/jpeg ' = 'jpeg ',
  6. ' image/jpg ' = 'jpg ',
  7. ' image/gif ' = 'gif ',
  8. ' image/png ' = 'png ',
  9. ' text/html ' = ' html ',
  10. ' text/plain 'and ' txt ',
  11. ' image/pjpeg ' = 'jpg ',
  12. ' image/x-png ' = 'png ',
  13. ' image/x-icon ' = 'ico '
  14. ];
  15. if (! Stristr ($url,' http ')) {
  16. $msg [' Code ']= 2101;
  17. $msg [' msg '] = ' URL address is incorrect! ';
  18. return $msg;
  19. }
  20. $dir = pathinfo ($url);
  21. //var_dump ($dir);
  22. $host = $dir [' dirname '];
  23. $refer = $host.'  /‘;
  24. $ch = curl_init ($url);
  25. curl_setopt ($ch, Curlopt_referer, $refer); //Forged Source address
  26. curl_setopt ($ch, Curlopt_returntransfer, 1); Return variable content or direct output string, 0 output, 1 return content
  27. curl_setopt ($ch, Curlopt_binarytransfer, 1); Return native (raw) output when Curlopt_returntransfer is enabled
  28. curl_setopt ($ch, Curlopt_header, 0); //whether to output header header information 0 No 1 Yes
  29. curl_setopt ($ch, curlopt_connecttimeout, $timeout); //Timeout period
  30. $data = curl_exec ($ch);
  31. //$httpCode = Curl_getinfo ($ch, Curlinfo_http_code);
  32. //$httpContentType = Curl_getinfo ($ch, Curlinfo_content_type);
  33. $info = curl_getinfo ($ch);
  34. Curl_close ($ch);
  35. $httpCode = intval ($info [' Http_code ']);
  36. $httpContentType = $info [' Content_Type '];
  37. $httpSizeDownload = intval ($info [' size_download ']);
  38. if ($httpCode! =') ' {
  39. $msg [' Code ']= 2102;
  40. $msg [' msg '] = ' url return content is incorrect!  ‘;
  41. return $msg;
  42. }
  43. if ($type >0 &&!isset ($imgs [$httpContentType]) {
  44. $msg [' Code ']= 2103;
  45. $msg [' msg '] = ' URL resource type unknown!  ‘;
  46. return $msg;
  47. }
  48. if ($httpSizeDownload <1) {
  49. $msg [' Code ']= 2104;
  50. $msg [' msg '] = ' content size is incorrect!  ‘;
  51. return $msg;
  52. }
  53. $msg [' code '] = 200;
  54. $msg [' status ']=' success ';
  55. $msg [' msg '] = ' resource acquisition success ';
  56. if ($type ==0 or $httpContentType = =' text/html ') $msg [' data '] = $data;
  57. $base _64 = base64_encode ($data);
  58. if ($type ==1) $msg [' data '] = $base _64;
  59. ElseIf ($type ==2) $msg [' data '] = "data:{$httpContentType};base64,{$base _64}";
  60. ElseIf ($type ==3) $msg [' data '] = "
  61. else $msg [' msg '] = ' unknown return requirement!     ‘;
  62. Unset ($info,$data,$base _64);
  63. return $msg;
  64. }
  65. $msg = Curl_url ($url, 3);
  66. if ($msg [' status ']==' success ') echo $msg [' data '];
  67. Die
  68. ?>

Php Curl Simple capture Image generation base64 code (with curl function parameter description)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.