Calling Interface of PHP Encapsulating curl and Detailed Explanation of Common Functions

  • 2021-10-15 10:11:00
  • OfStack

As shown below:


<?php
	/**
	 * @desc  Encapsulation curl The invocation interface of, post Request mode of 
	 */
	function doCurlPostRequest($url, $requestString, $timeout = 5) { 
		if($url == "" || $requestString == "" || $timeout <= 0){
			return false;
		}
		 $con = curl_init((string)$url);
	 curl_setopt($con, CURLOPT_HEADER, false);
	 curl_setopt($con, CURLOPT_POSTFIELDS, $requestString);
	 curl_setopt($con, CURLOPT_POST, true);
	 curl_setopt($con, CURLOPT_RETURNTRANSFER,true);
	 curl_setopt($con, CURLOPT_TIMEOUT, (int)$timeout);
		curl_setopt($con, CURLOPT_SSL_VERIFYPEER, false);
		curl_setopt($con, CURLOPT_SSL_VERIFYHOST, false);
	 return curl_exec($con);
	}
	
	/**
	 * @desc  Encapsulation curl The invocation interface of, post Request mode of 
	 */
	function doCurlPostsslRequest($url, $requestString,$pemfile, $timeout = 5) { 
		if($url == "" || $requestString == "" || $timeout <= 0){
			return false;
		}
		
		echo $pemfile;
		 $con = curl_init((string)$url);
	 curl_setopt($con, CURLOPT_HEADER, false);
	 curl_setopt($con, CURLOPT_POSTFIELDS, $requestString);
	 curl_setopt($con, CURLOPT_POST, true);
	 curl_setopt($con, CURLOPT_RETURNTRANSFER,true);
	 curl_setopt($con, CURLOPT_TIMEOUT, (int)$timeout);
		curl_setopt($con, CURLOPT_SSL_VERIFYPEER, 1);
		curl_setopt($con, CURLOPT_SSL_VERIFYHOST, 2);
		//curl_setopt($ch, CURLOPT_SSLCERT, $pemfile); 
		//curl_setopt($ch, CURLOPT_SSLCERTTYPE, 'PEM');
		curl_setopt($con, CURLOPT_CAINFO, $pemfile);
	 return curl_exec($con);
	} 
	/**
	 * @desc  Encapsulation curl The invocation interface of, post Json How to request data 
	 */
	function doCurlPostJsonReq($url, $requestString, $timeout = 25){
		if($url == "" || $requestString == "" || $timeout <= 0){
			return false;
		}
	 $con = curl_init((string)$url);
	 curl_setopt($con, CURLOPT_HEADER, false);
	 curl_setopt($con, CURLOPT_POSTFIELDS, $requestString);
	 curl_setopt($con, CURLOPT_POST, true);
	 curl_setopt($con, CURLOPT_RETURNTRANSFER,true);
	 curl_setopt($con, CURLOPT_TIMEOUT, (int)$timeout);	
		curl_setopt($con, CURLOPT_HTTPHEADER, array(
			'Content-Type: application/json',
			'Content-Length: ' . strlen($data_string))
		);
	 return curl_exec($con);
	}
	/**
	 * @desc  Encapsulation curl The invocation interface of, post form How to request data 
	 */
	function doCurlPostFormReq($url, $requestString, $timeout = 25){
		if($url == "" || $requestString == "" || $timeout <= 0){
			return false;
		}
	 $con = curl_init((string)$url);
	 curl_setopt($con, CURLOPT_HEADER, false);
	 curl_setopt($con, CURLOPT_POSTFIELDS, $requestString);
	 curl_setopt($con, CURLOPT_POST, true);
	 curl_setopt($con, CURLOPT_RETURNTRANSFER,true);
	 curl_setopt($con, CURLOPT_TIMEOUT, (int)$timeout);	
		curl_setopt($con, CURLOPT_HTTPHEADER, array(
			'Content-Type: application/x-www-form-urlencoded')
		);
	 return curl_exec($con);
	}
	/**
	 * @desc  Encapsulation curl The invocation interface of, get Request mode of 
	 */
	function doCurlGetRequest($url, $data = array(), $timeout = 10) {
		if($url == "" || $timeout <= 0){
			return false;
		}
		if($data != array()) {
			$url = $url . '?' . http_build_query($data);
		}
		Log::write(" Send URL[".$url."]");
		$con = curl_init((string)$url);
		curl_setopt($con, CURLOPT_HEADER, false);
		curl_setopt($con, CURLOPT_RETURNTRANSFER,true);
		curl_setopt($con, CURLOPT_TIMEOUT, (int)$timeout);
		curl_setopt($con, CURLOPT_SSL_VERIFYPEER, false);
		curl_setopt($con, CURLOPT_SSL_VERIFYHOST, false);
		return curl_exec($con);
	}
	/**
	 * Intercept string, Chinese without garbled code 
	*/
	function msubstr($str, $start=0, $length=0,$suffix=false,$charset="utf-8"){
	 if(mb_strlen($str,"UTF8") >= $length){
	  $next_str = " … ";
	 }else{
	  $next_str = "";
	 }
	 if(function_exists("mb_substr")){
	  if($suffix)
	   // return mb_substr($str, $start, $length, $charset)." … ";
	   return mb_substr($str, $start, $length, $charset).$next_str;
	  else
	   return mb_substr($str, $start, $length, $charset);
	 }elseif(function_exists('iconv_substr')) {
	  if($suffix)
	   // return iconv_substr($str,$start,$length,$charset)." … ";
	   return iconv_substr($str,$start,$length,$charset).$next_str;
	  else
	   return iconv_substr($str,$start,$length,$charset);
	 }
	 $re['utf-8'] = "/[x01-x7f]|[xc2-xdf][x80-xbf]|[xe0-xef][x80-xbf]{2}|[xf0-xff][x80-xbf]{3}/";
	 $re['gb2312'] = "/[x01-x7f]|[xb0-xf7][xa0-xfe]/";
	 $re['gbk'] = "/[x01-x7f]|[x81-xfe][x40-xfe]/";
	 $re['big5'] = "/[x01-x7f]|[x81-xfe]([x40-x7e]|xa1-xfe])/";
	 preg_match_all($re[$charset], $str, $match);
	 $slice = join("",array_slice($match[0], $start, $length));
	  if($suffix) return $slice." … ";
	 if(!$suffix) return $slice;
	 return $slice;
	}
	/**
	 * User name * Hide 
	 *$str = " Tathagata Palm ";
	 *echo cut_str($str, 1, 0).'**'.cut_str($str, 1, -1);
	 * Output: Such as ** Palm 
	*/
	function cut_str($string, $sublen, $start = 0, $code = 'UTF-8')
	{
	 if($code == 'UTF-8')
	 {
	  $pa = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/";
	  preg_match_all($pa, $string, $t_string);
	  if(count($t_string[0]) - $start > $sublen) return join('', array_slice($t_string[0], $start, $sublen));
	  return join('', array_slice($t_string[0], $start, $sublen));
	 }
	 else
	 {
	  $start = $start*2;
	  $sublen = $sublen*2;
	  $strlen = strlen($string);
	  $tmpstr = '';
	  for($i=0; $i< $strlen; $i++)
	  {
	   if($i>=$start && $i< ($start+$sublen))
	   {
	    if(ord(substr($string, $i, 1))>129)
	    {
	     $tmpstr.= substr($string, $i, 2);
	    }
	    else
	    {
	     $tmpstr.= substr($string, $i, 1);
	    }
	   }
	   if(ord(substr($string, $i, 1))>129) $i++;
	  }
	  //if(strlen($tmpstr)< $strlen ) $tmpstr.= "...";
	  return $tmpstr;
	 }
	}
	/*
	* Solve json_encode The problem of converting Chinese is in php5.4 Under json_encode This problem is solved by itself 
	*/
	function unescapedUnicode($match)
	{
		return mb_convert_encoding(pack('H*', $match[1]), 'UTF-8', 'UCS-2BE');
	}
	/**
	* Intercept mobile phone number 
	*demo:13112345678
	*return:131****5678
	*/
	function hidtel($phone){
	 $IsWhat = preg_match('/(0[0-9]{2,3}[-]?[2-9][0-9]{6,7}[-]?[0-9]?)/i',$phone); // Fixed telephone 
	 if($IsWhat == 1){
	  return preg_replace('/(0[0-9]{2,3}[-]?[2-9])[0-9]{3,4}([0-9]{3}[-]?[0-9]?)/i','$1****$2',$phone);
	 }else{
	  return preg_replace('/(1[358]{1}[0-9])[0-9]{4}([0-9]{4})/i','$1****$2',$phone);
	 }
	}
	// Get IP
	function getIp()
	{
		if (isset($_SERVER)){
			if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])){
				$realip = $_SERVER["HTTP_X_FORWARDED_FOR"];
			} else if (isset($_SERVER["HTTP_CLIENT_IP"])) {
				$realip = $_SERVER["HTTP_CLIENT_IP"];
			} else {
				$realip = $_SERVER["REMOTE_ADDR"];
			}
		} else {
			if (getenv("HTTP_X_FORWARDED_FOR")){
				$realip = getenv("HTTP_X_FORWARDED_FOR");
			} else if (getenv("HTTP_CLIENT_IP")) {
				$realip = getenv("HTTP_CLIENT_IP");
			} else {
				$realip = getenv("REMOTE_ADDR");
			}
		}
	
		return $realip;
	}
	// Convert Chinese characters into numbers 
	function getUnicodeFromOneUTF8($word)
	{
		// Gets the internal array representation of its characters, so this file applies the utf-8 Code ! 
		//$word = iconv("gb2312", "utf-8", $word);
		if (is_array($word)) 
			$arr = $word; 
		else 
			$arr = str_split($word); 
		// At this point, $arr Should be similar array(228, 189, 160) 
		// Definition 1 Empty string storage  
		$bin_str = ''; 
		// Turn into numbers and then into 2 Binary strings, finally combined.  
		foreach ($arr as $value) 
			$bin_str .= decbin(ord($value)); 
		// At this point, $bin_str Should be similar 111001001011110110100000
		// Regular interception  
		$bin_str = preg_replace('/^.{4}(.{4}).{2}(.{6}).{2}(.{6})$/','$1$2$3', $bin_str);
		// At this point,  $bin_str Should be similar 0100111101100000, If it is a Chinese character " You "
		return bindec($bin_str); 
		// Returns a similar 20320 ,   Chinese characters " You " 
		//return dechex(bindec($bin_str)); 
		// If you want to return 106 Binary system 4f60 , use this sentence  

Related articles: