Post and get classes for PHP encapsulated Curl

Source: Internet
Author: User
<pre name= "code" class= "PHP" &GT;&LT;? Class curl{/** * Get method * * @param string interface address * @param string endpoint * @param string model name * @param stri 
		Ng Method Name * @param array parameter * @return JSON */static public function Sendbyget ($host, $data) {$fields _string = ';
			if (!empty ($data)) {ksort ($data);
			$fuhao = ';
				foreach ($data as $k => $v) {$fields _string. = $fuhao. $v;
			$fuhao = '/'; } if (!empty ($fields _string)) {$url = $host. '
        /'. $fields _string;
        else {$url = $host;
		} $ch = Curl_init ();
		curl_setopt ($ch, Curlopt_url, $url);
		curl_setopt ($ch, Curlopt_ipresolve, CURL_IPRESOLVE_V4);
		curl_setopt ($ch, Curlopt_header, 0);
		curl_setopt ($ch, Curlopt_fresh_connect, 1);
		curl_setopt ($ch, Curlopt_returntransfer, 1);
        curl_setopt ($ch, Curlopt_timeout, 5);
		curl_setopt ($ch, curlopt_http_version, CURL_HTTP_VERSION_1_0);

        $str = curl_exec ($ch); $status = Curl_getinfo ($ch, Curlinfo_htTp_code);
   		Curl_close ($ch);
		if ($status = =) {return $str;
		else {return false; }/** * Post method * * @param string interface address * @param array parameter * @return JSON/public F
		Unction sendbypost ($url, $data, $cookie = ') {$ch = Curl_init ();
		curl_setopt ($ch, Curlopt_url, $url);
		if ($cookie) {curl_setopt ($ch, Curlopt_cookie, $cookie);
		curl_setopt ($ch, Curlopt_header, 0);
		curl_setopt ($ch, Curlopt_returntransfer, 1);
		curl_setopt ($ch, Curlopt_post, true);
		curl_setopt ($ch, Curlopt_connecttimeout, 120);
        curl_setopt ($ch, curlopt_useragent, $_server[' http_user_agent ']);
		curl_setopt ($ch, curlopt_http_version, CURL_HTTP_VERSION_1_0);
		curl_setopt ($ch, Curlopt_postfields, $data);
		$str = curl_exec ($ch); if (Curl_errno ($ch)) {$error = "Curl Error:". Curl_error ($ch). " \ n: ". Date (' y-m-d h:i:s ', Time ())."
			 \ r \ n ";
		File_put_contents (' Curl_post_error.txt ', $error, file_append);
		} curl_close ($ch); ReTurn $str; }
}

When you obtain the HTTPS protocol request, you need to add




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.