PHP HTTP Request Curl Method Curl HTTP Get Curl PHP curlopt Httpheade

Source: Internet
Author: User
 host = $host;     }/** * GET wrappwer for request. * * @return Mixed * * function get ($url, $parameters = Array (), $headers = Array (), $cookie = Array ()) {$        Response = $this->request ($url, ' GET ', $parameters, NULL, $headers, $cookie);        if ($this->format = = = ' json ' && $this->decode_json) {return Json_decode ($response, true);    } return $response;     }/** * POST wreapper for request. * * @return Mixed */function post ($url, $parameters = Array (), $multi = false, $headers = Array (), $cookie = AR        Ray ()) {$response = $this->request ($url, ' POST ', $parameters, $multi, $headers, $cookie);        if ($this->format = = = ' json ' && $this->decode_json) {return Json_decode ($response, true);    } return $response;     }/** * Delte wrapper for Oauthreqeust. * * @return Mixed */function Delete ($url, $parameters = Array ()) {$response= $this->request ($url, ' DELETE ', $parameters);        if ($this->format = = = ' json ' && $this->decode_json) {return Json_decode ($response, true);    } return $response; /** * Format and sign an OAUTH/API request * * @return String * @ignore */function request ( $url, $method, $parameters, $multi = false, $headers = Array (), $cookie = Array ()) {if (Strrpos ($url, ' http.//')! =        = 0 && Strrpos ($url, ' https://')!== 0) {$url = "{$this->host}{$url}"; } switch ($method) {case ' GET ': $url. = Strpos ($url, '? ') = = = False?                '?' : '';                $url. = Http_build_query ($parameters);            return $this->http ($url, ' GET ', null, $headers, $cookie);                Default://$headers = Array ();                $body = $parameters; if ($multi) {$body = Self::build_http_query_multi ($parameteRS); $headers [] = "content-type:multipart/form-data; Boundary= ".                Self:: $boundary; } elseif (Is_array ($parameters) | | is_object ($parameters)) {if (In_array (' Content-type:applic Ation/json ', $headers) | | In_array (' Content-type:application/json ', $headers)) {$body = Json_encode ($parameters);}                else{$body = Http_build_query ($parameters);}        } return $this->http ($url, $method, $body, $headers, $cookie); }}/** * make an HTTP request * * @return string API results * @ignore */function HTTP ($url        , $method, $postfields = NULL, $headers = Array (), $cookie = Array ()) {$this->http_info = array ();        $ci = Curl_init ();        /* Curl Settings */curl_setopt ($CI, curlopt_http_version, CURL_HTTP_VERSION_1_0);        curl_setopt ($ci, curlopt_useragent, $this->useragent);       curl_setopt ($ci, Curlopt_connecttimeout, $this->connecttimeout); curl_setopt ($ci, Curlopt_timeout, $this->timeout);        curl_setopt ($ci, Curlopt_returntransfer, TRUE);        curl_setopt ($ci, Curlopt_encoding, "");        curl_setopt ($ci, Curlopt_ssl_verifypeer, $this->ssl_verifypeer);        curl_setopt ($ci, Curlopt_headerfunction, Array ($this, ' GetHeader '));        curl_setopt ($ci, Curlopt_header, FALSE);                Switch ($method) {case ' POST ': curl_setopt ($ci, Curlopt_post, TRUE);                    if (!empty ($postfields)) {curl_setopt ($ci, Curlopt_postfields, $postfields);                $this->postdata = $postfields;            } break;                Case ' Delete ': curl_setopt ($ci, curlopt_customrequest, ' delete '); if (!empty ($postfields)) {$url = "{$url}?{                $postfields} "; }} $headers [] = "API-REMOTEIP:".        $_server[' REMOTE_ADDR '];        curl_setopt ($ci, Curlopt_url, $url); curl_setopt($ci, Curlopt_httpheader, $headers);        curl_setopt ($ci, Curlinfo_header_out, TRUE);        if ($this->referer) {curl_setopt ($ci, Curlopt_referer, $this->referer);        } if ($this->follow) {curl_setopt ($ci, curlopt_followlocation, 1);        } if (!empty ($cookie)) {$str = "";                foreach ($cookie as $key = = $value) {$str. = "{$key}={$value};";}        $str = Trim ($str, '; ');        curl_setopt ($ci, Curlopt_cookie, $STR);        } $response = Curl_exec ($CI);        $this->http_code = Curl_getinfo ($ci, Curlinfo_http_code);                $this->http_info = Array_merge ($this->http_info, Curl_getinfo ($CI)); if ($this->http_code! =) {throw new Exception (Json_encode ($this->http_info), $this->http_code)        ;        } $this->url = $url;        Curl_close ($CI);    return $response;    }    /** * Get the header info to store.        * * @return int * @ignore */function GetHeader ($ch, $header) {$i = Strpos ($header, ': ');            if (!empty ($i)) {$key = Str_replace ('-', ' _ ', Strtolower (substr ($header, 0, $i)));            $value = Trim (substr ($header, $i + 2));        $this->http_header[$key] = $value;    } return strlen ($header); /** * Processing Multimedia Data Contents * @ignore */public static function Build_http_query_multi ($params) {if (! $par        AMS) return ';        Uksort ($params, ' strcmp ');        $pairs = Array ();        Self:: $boundary = $boundary = Uniqid ('------------------');        $MPboundary = '--'. $boundary; $endMPboundary = $MPboundary.        '--';        $multipartbody = "; foreach ($params as $parameter = + $value) {if (In_array ($parameter, Array (' pic ', ' image ', ' Filedata ')) &                & $value {0} = = ' @ ') {$url = LTrim ($value, ' @ '); $content = File_geT_contents ($url);                $array = Explode ('? ', basename ($url));                $filename = $array [0]; $multipartbody. = $MPboundary.                "\ r \ n"; $multipartbody. = ' Content-disposition:form-data; Name= "'. $parameter. '"; Filename= "'. $filename. '"'. "                \ r \ n ";                $multipartbody. = "content-type:image/unknown\r\n\r\n"; $multipartbody. = $content.            "\ r \ n"; } else {$multipartbody. = $MPboundary.                "\ r \ n"; $multipartbody. = ' Content-disposition:form-data; Name= "'. $parameter.                "\" \r\n\r\n "; $multipartbody. = $value. "            \ r \ n ";        }} $multipartbody. = $endMPboundary;    return $multipartbody;     }/** * Bulk Request Urlget * Enter description here ... * @param unknown_type $urls * @return Array */ Public Function Batch_get ($url _arr) {$mh = Curl_multi_init (); foreach ($url _arr as $i = + $url) {$conn [$i ]=curl_iNIT ($url); curl_setopt ($conn [$i],curlopt_returntransfer,1);//settings return do.php page Output curl_multi_add_handle ($MH, $conn [$i]);//Add Thread} do {$MRC = Curl_multi_exec ($MH, $active),} while ($MRC = = Curlm_call_multi_perform), while ($active and $MRC = = Curlm_o         K) {if (Curl_multi_select ($MH)! =-1) {do {$MRC = Curl_multi_exec ($MH, $active);         } while ($MRC = = Curlm_call_multi_perform); }}foreach ($url _arr as $i + $url) {$res [$i]=curl_multi_getcontent ($conn [$i]);//Get page input curl_close ($conn [$i]);}    return $res; }    }

The above describes the PHP HTTP request Curl method, including the curl,http aspects of the content, I hope that the PHP tutorial interested in a friend to help.

  • 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.