- 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
- <?php
- $url ="http://c.hiphotos.baidu.com/image/w%3D210/sign=ed30880babec8a13141a50e1c7029157/ D52a2834349b033be1a9503e17ce36d3d539bd35.gif ";
- function Curl_url ($url,$type =0,$timeout =30) {
- $msg = [' code ' =>2100,' status ' = 'error ',' msg ' = ' =' unknown error! ‘];
- $imgs = [' image/jpeg ' = 'jpeg ',
- ' image/jpg ' = 'jpg ',
- ' image/gif ' = 'gif ',
- ' image/png ' = 'png ',
- ' text/html ' = ' html ',
- ' text/plain 'and ' txt ',
- ' image/pjpeg ' = 'jpg ',
- ' image/x-png ' = 'png ',
- ' image/x-icon ' = 'ico '
- ];
- if (! Stristr ($url,' http ')) {
- $msg [' Code ']= 2101;
- $msg [' msg '] = ' URL address is incorrect! ';
- return $msg;
- }
- $dir = pathinfo ($url);
- //var_dump ($dir);
- $host = $dir [' dirname '];
- $refer = $host.' /‘;
- $ch = curl_init ($url);
- curl_setopt ($ch, Curlopt_referer, $refer); //Forged Source address
- curl_setopt ($ch, Curlopt_returntransfer, 1); Return variable content or direct output string, 0 output, 1 return content
- curl_setopt ($ch, Curlopt_binarytransfer, 1); Return native (raw) output when Curlopt_returntransfer is enabled
- curl_setopt ($ch, Curlopt_header, 0); //whether to output header header information 0 No 1 Yes
- curl_setopt ($ch, curlopt_connecttimeout, $timeout); //Timeout period
- $data = curl_exec ($ch);
- //$httpCode = Curl_getinfo ($ch, Curlinfo_http_code);
- //$httpContentType = Curl_getinfo ($ch, Curlinfo_content_type);
- $info = curl_getinfo ($ch);
- Curl_close ($ch);
- $httpCode = intval ($info [' Http_code ']);
- $httpContentType = $info [' Content_Type '];
- $httpSizeDownload = intval ($info [' size_download ']);
- if ($httpCode! =') ' {
- $msg [' Code ']= 2102;
- $msg [' msg '] = ' url return content is incorrect! ‘;
- return $msg;
- }
- if ($type >0 &&!isset ($imgs [$httpContentType]) {
- $msg [' Code ']= 2103;
- $msg [' msg '] = ' URL resource type unknown! ‘;
- return $msg;
- }
- if ($httpSizeDownload <1) {
- $msg [' Code ']= 2104;
- $msg [' msg '] = ' content size is incorrect! ‘;
- return $msg;
- }
- $msg [' code '] = 200;
- $msg [' status ']=' success ';
- $msg [' msg '] = ' resource acquisition success ';
- if ($type ==0 or $httpContentType = =' text/html ') $msg [' data '] = $data;
- $base _64 = base64_encode ($data);
- if ($type ==1) $msg [' data '] = $base _64;
- ElseIf ($type ==2) $msg [' data '] = "data:{$httpContentType};base64,{$base _64}";
- ElseIf ($type ==3) $msg [' data '] = "
- else $msg [' msg '] = ' unknown return requirement! ‘;
- Unset ($info,$data,$base _64);
- return $msg;
- }
- $msg = Curl_url ($url, 3);
- if ($msg [' status ']==' success ') echo $msg [' data '];
- Die
- ?>
Php Curl Simple capture Image generation base64 code (with curl function parameter description)