Php-curl Package [Avoid file ambiguity bug]

Source: Internet
Author: User
Tags sprintf
Php-curl package [Avoid uploading files ambiguity bug]

Since PHP's curl is in curl_setopt ($curl, curlopt_postfields, xxx), when XXX is an array, if the first character of the value is @, then it is considered a file upload, when you need to upload the file, [email protected] , there is a conflict. Therefore, the setup of the post data in the api_common.php is encapsulated


  TRUE, request_behavior_max_redirect=> 5, request_behavior_user_agent=> ' Curl-lib ', request_behavior_ Autoreferer=> TRUE, request_behavior_upload=> FALSE, request_behavior_connecttimeout=> 3, REQUEST_BEHAVIOR_ Dns_cache_timeout=> 3600, request_behavior_timeout=> 3, request_behavior_encoding=> ' gzip ', REQUEST_ Behavior_error_handler=> ' __default_curl_error_handler ',);d efine (' multipart_form_data_head_fmt ', ' Content-type:multipart/form-data; boundary=----------------------------%s ');d efine (' multipart_form_data_body_string ', "-------------------------- ----%s\r\ncontent-disposition:form-data; Name=\ "%s\" \r\n\r\n%s\r\n ");d efine (' Multipart_form_data_body_file ',"------------------------------%s\r\ Ncontent-disposition:form-data; Name=\ "%s\"; Filename=\ "%s\" \r\ncontent-type:application/octet-stream\r\n\r\n%s\r\n ");d efine (' Multipart_form_data_body_end ') , "------------------------------%s--\r\n\r\n"), #响应键值define (' Resp_code ', ' Resp_code ');d efine (' resp_body ', ' resp_ Body ');d EFINE (' resp_header ', ' Resp_header '); #HTTP 1xx Status Verification define (' Http_1xx_resp ', '/^http\/1.[ 1\D{2} \w+/'); #默认错误处理的错误消息define (' e_curl_error_fmt ', ' CURL '%s ' error[%d]:%s '); #默认的curl错误处理function __default_ Curl_error_handler ($curl, $url, $errno, $errstr) {Trigger_error (sprintf (e_curl_error_fmt, $url, $errno, $errstr), E_ User_error);} #切换CURL请求方法function __method_switch ($curl, $method) {switch ($method) {case request_method_post:__curl_setopt ($curl, Curlopt_post, True); Break;case request_method_head:__curl_setopt ($curl, Curlopt_nobody, TRUE); Break;case REQUEST_ Method_get:__curl_setopt ($curl, Curlopt_httpget, TRUE); break;default:break;}} #设置默认头信息function __default_header_set ($curl) {__curl_setopt ($curl, curlopt_returntransfer,true); __curl_setopt ($ Curl, curlopt_header,true); __curl_setopt ($curl, curlopt_followlocation, (BOOL) Curl_behavior (request_behavior_ Allow_redirect)); __curl_setopt ($curl, Curlopt_maxredirs, (int) curl_behavior (request_behavior_max_redirect)); __ curl_setopt ($curl, curlopt_useragent, (string) Curl_behavior (request_behavior_user_agent)); __curl_setopt ($curl, Curlopt_autoreferer, (BOOL) Curl_behavior ( Request_behavior_autoreferer)); __curl_setopt ($curl, Curlopt_upload, (BOOL) Curl_behavior (Request_behavior_upload) ); __curl_setopt ($curl, curlopt_connecttimeout, (int) curl_behavior (request_behavior_connecttimeout)); __curl_ Setopt ($curl, curlopt_dns_cache_timeout, (int) curl_behavior (request_behavior_dns_cache_timeout)); __curl_setopt ($ Curl, Curlopt_timeout, (int) curl_behavior (request_behavior_timeout)); __curl_setopt ($curl, Curlopt_encoding, ( String) Curl_behavior (request_behavior_encoding));}  #设置用户自定义头信息function __custom_header_set ($curl, $headers = NULL) {if (empty ($headers)) return; if (is_string ($headers)) $headers = Explode ("\ r \ n", $headers), #类型修复foreach ($headers as & $header) if (Is_array ($header)) $header = sprintf (' %s:%s ', $header [0], $header [1]); __curl_setopt ($curl, Curlopt_httpheader, $headers);} #设置请求bodyfunction __datas_set ($curl, $datas = NULL) {if (empty ($datas)) retUrn, if (Is_array ($datas)) {$custom _body= FALSE; $uniqid = Uniqid (); $custom _body_str= "; foreach ($datas as $name = $ Data) {if (Is_array ($data) && array_key_exists (Api_curl_upload_file, $data)) {$file = $data [Api_curl_upload_ File];if (File_exists ($file)) {$custom _body= TRUE; $custom _body_str.= sprintf (Multipart_form_data_body_file, $uniqid , $name, $file, file_get_contents ($file));}} else {$custom _body_str.= sprintf (multipart_form_data_body_string, $uniqid, $name, $data);}} if ($custom _body) {curl_setopt ($curl, Curlopt_httpheader, Array (sprintf (MULTIPART_FORM_DATA_HEAD_FMT, $uniqid))); $ datas= $custom _body_str. sprintf (Multipart_form_data_body_end, $uniqid);}} __curl_setopt ($curl, Curlopt_postfields, $datas);} #对curl_setopt的封装function __curl_setopt ($curl, $optname, $optval) {curl_setopt ($curl, $optname, $optval); __curl_error ($curl);} #curl错误检查处理function __curl_error ($curl) {if (Curl_errno ($curl)) {$url = Curl_getinfo ($curl, Curlinfo_effective_url); $ errno= Curl_errno ($curl); $errstr = Curl_error ($curl); $errh = Curl_behavior (Request_behavior_error_handler); if (Function_exists ($ERRH)) $ Errh ($curl, $url, $errno, $ERRSTR);}} #api默认行为切换function Curl_behavior ($names, $values = NULL) {if (!is_string ($names) &&!is_array ($names)) return; F (!is_null ($values)) {if (is_string ($names)) $GLOBALS [request_behaviors][$names]= $values; else if (Is_array ($names) &&!is_array ($values)) foreach ($names as $name) $GLOBALS [request_behaviors][$name]= $values; else if (Is_array ( $names) && Is_array ($values)) foreach ($names as $k = $name) $GLOBALS [request_behaviors][$name]= $values [$k] ;} if (is_string ($names)) {$return = $GLOBALS [request_behaviors][$names];} else if (Is_array ($names)) {$return = array (); fo Reach ($names as $name) $return [$name]= array_key_exists ($name, $GLOBALS [request_behaviors])? $GLOBALS [request_behaviors][$name]: NULL;} return $return;} #请求入口function curl_request ($url, $method, $datas = null, $headers = null) {$curl = curL_init ($url); __method_switch ($curl, $method), __default_header_set ($curl); __custom_header_set ($curl, $headers); __ Datas_set ($curl, $datas); $response = Curl_exec ($curl); __curl_error ($curl); $status _code= Curl_getinfo ($curl, Curlinfo_http_code); $components = explode ("\r\n\r\n", $response), $i = -1;while (+ + $i < count ($components)) if (!preg_ Match (Http_1xx_resp, $components [$i])) break; $headers = $components [$i]; $body = implode ("\r\n\r\n", Array_slice ($ components, $i + 1)); return Array (resp_code=> $status _code, resp_header=> $headers, resp_body=> $body,);} #GET请求function Curl_get ($url, $headers = null) {return curl_request ($url, Request_method_get, NULL, $headers);} #POST请求function curl_post ($url, $datas = null, $headers = null) {return curl_request ($url, Request_method_post, $datas, $h eaders);} #HEAD请求function Curl_head ($url, $headers = null) {return curl_request ($url, Request_method_head, NULL, $headers);} #构造上传文件字段function Curl_post_file ($file) {return Array (api_curl_upload_file=&Gt $file,);} #读取响应码function Curl_resp_code ($RESP) {return $resp [Resp_code];} #读取响应头function Curl_resp_header ($RESP) {return $resp [Resp_header];} #读取响应体function Curl_resp_body ($RESP) {return $resp [resp_body];}


  • Related Article

    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.