PHP transmission session curl function instance details, session curl

Source: Internet
Author: User

PHP transmission session curl function instance details, session curl

Example of the PHP transfer session curl Function

Preface:

Take over the burden of the company's project PC end owner, and take a major responsibility; from requirement analysis, drawing flowcharts, creating tables, coding, testing and fixing bugs, online maintenance will be completed by a single person in charge of a light pole (of course there is also a good front-end technical cooperation, thanks to the help of the supervisor). Although I am tired of working overtime, I feel okay, the company is a bird.

I will not talk about it much, because the interfaces on the java side are often called in the project. Since the request interfaces are involved, the http request method is available, PHP is commonly used for GET/POST. Of course, there are other methods such as put, and GET/POST/PUT/DELETE are often used in java, of course, curl-related functions are required for the request interface. I hope you can read the documentation for debugging. Below are the encapsulated related functions (which have been called in summary ):

Sample Code:

Private $ serverhost = "https://demo.xxx.cn "; // test/*** request interface encapsulate * access public * @ param string $ url interface address, such as get/post/put/delete, * @ param string $ params parameter * @ param string $ type get/post/put/delete * @ return bool/array */public function getcurldata ($ url, $ params, $ type = "get") {$ url = $ this-> serverhost. $ url; $ response = array (); if ($ type = 'get ') {// get request // The request header can be added with other settings $ headers = array ('content-type: application/json; charset = UTF-8 ',); $ ch = curl_init (); curl_setopt ($ ch, CURLOPT_URL, $ url); curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, true); curl_setopt ($ ch, CURLOPT_FOLLOWLOCATION, TRUE ); curl_setopt ($ ch, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt ($ ch, CURLOPT_HTTPHEADER, $ headers); $ response = curl_exec ($ ch );} elseif ($ type = 'post') {// post request $ headers = array ('content-type: application/json; charset = UTF-8 ',); $ ch = curl_init (); curl_setopt ($ ch, CURLOPT_URL, $ url); curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, true); curl_setopt ($ ch, CURLOPT_FOLLOWLOCATION, TRUE ); curl_setopt ($ ch, CURLOPT_POST, true); // note these rows curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ params); // note these rows // curl_setopt ($ ch, CURLOPT_HEADER, true); curl_setopt ($ ch, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt ($ ch, CURLOPT_HTTPHEADER, $ headers); $ response = curl_exec ($ ch );} elseif ($ type = 'put') {// put request $ headers = array ('content-type: application/json; charset = UTF-8 ',); $ ch = curl_init (); curl_setopt ($ ch, CURLOPT_URL, $ url); curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, true); curl_setopt ($ ch, CURLOPT_FOLLOWLOCATION, TRUE ); curl_setopt ($ ch, CURLOPT_PUT, true); // note these rows curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ params); // curl_setopt ($ ch, CURLOPT_HEADER, true ); curl_setopt ($ ch, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt ($ ch, CURLOPT_HTTPHEADER, $ headers); $ response = curl_exec ($ ch);} return $ response ;} // how to call the above Code // get method/*** query the class I have created * @ param string $ url interface address * @ param string $ params parameter * @ param string $ type get * @ return array */public function mycreateclass ($ userid) {$ url = "/xxx/xxxx /". $ userid; // request address splicing $ response = $ this-> getcurldata ($ url, array (), "get"); $ createdclass = json_decode ($ response, true ); // return json format data return $ createdclass ;} /** post Request * User Logon judgment * access public * @ param string $ username * @ param string $ password * @ return bool */public function getlogin ($ username, $ password) {// data to be post $ params = array ("username" => $ username, "password" => $ password); $ params = json_encode ($ params, 64 | 256); $ uri = "/xxx/login"; $ response = $ this-> getcurldata ($ uri, $ params, "post "); $ result = json_decode ($ response, true); return $ result;}/* ID conversion-put Request */public function changeuserole ($ token) {// data to be put $ params = array (); $ params = json_encode ($ params, 64 | 256); $ uri = "/xxx /". $ token. "/"; $ response = $ this-> getcurldata ($ uri, $ params, "put"); $ result = json_decode ($ response, true ); // dump ($ result); die; return $ result;} // there is also a delete method. You can refer to the document for debugging. The above three request methods are all single requests (that is, one request) * ************** PHP built-in function curl_multi_get_contents function (thinkphp comes with this function, you can fine-tune it ): /*** the batch request has been called through * curl multi POST Data **/public function curl_multi_get_contents ($ url = array (), $ param = array (), $ timeout = 1000) {$ userAgent = 'mozilla/4.0 + (compatible; + MSIE + 6.0; + Windows + NT + 5.1; + SV1) '; $ headers = array ('content-type: application/json; charset = UTF-8 ',); $ curl_array = array (); $ mh = curl_multi_init (); foreach ($ url as $ uk => $ uv) {$ curl_array [$ uk] = curl_init ();} unset ($ uk, $ uv); foreach ($ url as $ uk => $ uv) {$ options = array (CURLOPT_URL => $ uv, CURLOPT_TIMEOUT => $ timeout, CURLOPT_RETURNTRANSFER => true, timeout => 86400, timeout => true, CURLOPT_SSL_VERIFYPEER => 0, CURLOPT_HEADER => false, CURLOPT_USERAGENT => 'mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0) ', CURLOPT_HTTPHEADER => $ headers ,); if (isset ($ param [$ uk]) {foreach ($ param [$ uk] as $ _ k = >$ _ v) {// $ options [$ _ k] =$ _ v; $ optionsparam [$ _ k] =_ _ v; $ options [CURLOPT_POSTFIELDS] = json_encode ($ optionsparam, 64 | 256) ;}} curl_setopt_array ($ curl_array [$ uk], $ options); curl_multi_add_handle ($ mh, $ curl_array [$ uk]); unset ($ options );} unset ($ uk, $ uv); $ running = NULL; do {curl_multi_exec ($ mh, $ running);} while ($ running> 0 ); $ res = array (); foreach ($ url as $ uk => $ uv) {$ res [$ uk] = curl_multi_getcontent ($ curl_array [$ uk]);} unset ($ uk, $ uv); foreach ($ url as $ uk = >$ uv) {curl_multi_remove_handle ($ mh, $ curl_array [$ uk]);} unset ($ url, $ curl_array, $ uk, $ uv); curl_multi_close ($ mh); return $ res ;} // how to call -- initiate a request in batches // Add the request to the class public function batchjoinclass ($ token, $ batchjoinclass) {$ urlarr = $ param = $ returndata = array (); $ param = $ batchjoinclass; // the two-dimensional array format is as follows/* $ param [1] ['name'] = 'class new 1 '; $ param [1] ['iamge'] = 'xxx11.png '; $ param [1] ['iamgetype'] = 'custom '; $ param [2] ['name'] = 'new class 2'; $ param [2] ['iamge'] = 'xxx.png '; $ param [2] ['iamgetype '] = 'custom'; * // obtain the request url foreach ($ batchjoinclass as $ key => $ val) {$ urlarr [$ key] = $ this-> serverhost. "/xxx/xxxx /". $ token;} $ res = $ this-> curl_multi_get_contents ($ urlarr, $ param); // format the returned data foreach ($ res as $ key => $ val) {$ returndata [$ key] = json_decode ($ val, true);} return $ returndata ;}

If you have any questions, please leave a message or go to the community on this site for discussion. Thank you for reading this article. Thank you for your support!

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.