PHP Transfer Session Curl function

Source: Internet
Author: User
This article mainly introduces the PHP transfer session Curl Function Example of the relevant information, hope that through this article can help everyone, the need for friends can refer to the next

Example of a PHP transfer session curl function

Objective:

Take over the responsibility of the PC owner of the company project. From the requirements analysis, painting flowchart, build table, code, test fix bug, on-line maintenance and so I a guanggansiling a person to complete (of course, there is a good technical front-end cooperation, thank the Director of the Help), although tired point overtime more but feel OK, The company is a bird-like.

Gossip not much to say, because the project often need to tune the Java side of the interface, since it involves the request interface that has the HTTP request way, PHP Common is get/post Two of course there are other such as PUT, Java is often used to get/post/put/ Delete, and so on, the request interface of course to use the related functions of curl, are looking at the document debugging hope that we all look at the document, the following is my package of relevant functions and so on (probably summed up, has been transferred):

Example code:

    Private $serverhost = "https://demo.xxx.cn"; Test/** * Request Interface Encapsulation Get/post/put/delete ETC * Access public * @param string $url interface address * @param string $para Ms parameter * @param string $type type Get/post/put/delete * @return Bool/array */Public function Getcurldata ($ur         L, $params, $type = "Get") {$url = $this->serverhost. $url;        $response = Array (); if ($type = = ' Get ') {//get request//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 lines of curl_setopt ($ch, Curlopt_postfields, $params);         Note These lines of//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 lines of 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 way/** * Query the Class I have created * @param string $url interface address * @param string $params parameter * @param string $type type Get * @return Array */Public function Mycreateclass ($userid) {$url = "/xxx/xxxx/xxxx/". $userid;       Request Address Stitching $response = $this->getcurldata ($url, Array (), "get"); $createdclass = Json_decode ($response, true);    Returns the JSON formatted data return $createdclass;     }/** Post mode request * User logon judgment * Access public * @param string $username username * @param string $password password          * @return BOOL */Public Function GetLogin ($username, $password) {//data to post $params = Array (      "Username" = $username, "password" and "$password");      $params = Json_encode ($params, 64|256); $uri = "/xxx/xxx/login ";      $response = $this->getcurldata ($uri, $params, "post");       $result = Json_decode ($response, true);    return $result;        }/* Identity conversion--put Request */Public Function Changeuserole ($token) {//data to put $params = array ();         $params = Json_encode ($params, 64|256); $uri = "/xxx/xxx/xxx/". $token. "        /";        $response = $this->getcurldata ($uri, $params, "put");         $result = Json_decode ($response, true);         Dump ($result);d ie;     return $result; }//There is also a Delete method for you to refer to the documentation for yourself. The above 3 request methods are a single request (that is, request once) ***************php self-curl_multi_get_contents function (thinkphp this function, Can be fine-tuned):/** * Volume initiation request has been tuned * Curl Multi POST Data * */Public Function curl_multi_get_contents ($url =arr         Ay (), $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, curlop  T_timeout = $timeout, Curlopt_returntransfer = true, Curlopt_dns_cache_timeout =                86400, Curlopt_dns_use_global_cache = 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--batch initiate request//batch request join class Public Function Batchjoinclass ($token, $batchjoinclass) {$urlarr = $param = $retur         Ndata = 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 '] = ' class new 2 ';         $param [2][' iamge '] = ' xxx.png '; $param [2][' iamgetype '] = ' CUSTOM '; *//Get request URL foreach ($batchjoinclass as $key + = $val) {$urlarr [$key] = $this->serverhost. "        /xxx/xxxx/xxxx/". $token;         } $res = $this->curl_multi_get_contents ($urlarr, $param);        Formatted return data foreach ($res as $key + = $val) {$returndata [$key] = Json_decode ($val, true);    } return $returndata; }

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.