ホームページ >バックエンド開発 >PHPチュートリアル >PHP は、curl の呼び出しインターフェイスと共通関数をカプセル化します。

PHP は、curl の呼び出しインターフェイスと共通関数をカプセル化します。

WBOY
WBOYオリジナル
2016-07-28 08:28:54940ブラウズ
<span><?</span><span>php
	</span><span>/**
	 * @desc 封装curl的调用接口,post的请求方式
	 */</span><span>function</span><span> doCurlPostRequest</span><span>(</span><span>$url</span><span>,</span><span> $requestString</span><span>,</span><span> $timeout </span><span>=</span><span>5</span><span>)</span><span>{</span><span>if</span><span>(</span><span>$url </span><span>==</span><span>""</span><span>||</span><span> $requestString </span><span>==</span><span>""</span><span>||</span><span> $timeout </span><span><=</span><span>0</span><span>){</span><span>return</span><span>false</span><span>;</span><span>}</span><span>
		 $con </span><span>=</span><span> curl_init</span><span>((</span><span>string</span><span>)</span><span>$url</span><span>);</span><span>
	    curl_setopt</span><span>(</span><span>$con</span><span>,</span><span> CURLOPT_HEADER</span><span>,</span><span>false</span><span>);</span><span>
	    curl_setopt</span><span>(</span><span>$con</span><span>,</span><span> CURLOPT_POSTFIELDS</span><span>,</span><span> $requestString</span><span>);</span><span>
	    curl_setopt</span><span>(</span><span>$con</span><span>,</span><span> CURLOPT_POST</span><span>,</span><span>true</span><span>);</span><span>
	    curl_setopt</span><span>(</span><span>$con</span><span>,</span><span> CURLOPT_RETURNTRANSFER</span><span>,</span><span>true</span><span>);</span><span>
	    curl_setopt</span><span>(</span><span>$con</span><span>,</span><span> CURLOPT_TIMEOUT</span><span>,</span><span>(</span><span>int</span><span>)</span><span>$timeout</span><span>);</span><span>
		curl_setopt</span><span>(</span><span>$con</span><span>,</span><span> CURLOPT_SSL_VERIFYPEER</span><span>,</span><span>false</span><span>);</span><span>
		curl_setopt</span><span>(</span><span>$con</span><span>,</span><span> CURLOPT_SSL_VERIFYHOST</span><span>,</span><span>false</span><span>);</span><span>return</span><span> curl_exec</span><span>(</span><span>$con</span><span>);</span><span>}</span><span>/**
	 * @desc 封装curl的调用接口,post的请求方式
	 */</span><span>function</span><span> doCurlPostsslRequest</span><span>(</span><span>$url</span><span>,</span><span> $requestString</span><span>,</span><span>$pemfile</span><span>,</span><span> $timeout </span><span>=</span><span>5</span><span>)</span><span>{</span><span>if</span><span>(</span><span>$url </span><span>==</span><span>""</span><span>||</span><span> $requestString </span><span>==</span><span>""</span><span>||</span><span> $timeout </span><span><=</span><span>0</span><span>){</span><span>return</span><span>false</span><span>;</span><span>}</span><span>		echo $pemfile</span><span>;</span><span>		 $con </span><span>=</span><span> curl_init</span><span>((</span><span>string</span><span>)</span><span>$url</span><span>);</span><span>
	    curl_setopt</span><span>(</span><span>$con</span><span>,</span><span> CURLOPT_HEADER</span><span>,</span><span>false</span><span>);</span><span>
	    curl_setopt</span><span>(</span><span>$con</span><span>,</span><span> CURLOPT_POSTFIELDS</span><span>,</span><span> $requestString</span><span>);</span><span>
	    curl_setopt</span><span>(</span><span>$con</span><span>,</span><span> CURLOPT_POST</span><span>,</span><span>true</span><span>);</span><span>
	    curl_setopt</span><span>(</span><span>$con</span><span>,</span><span> CURLOPT_RETURNTRANSFER</span><span>,</span><span>true</span><span>);</span><span>
	    curl_setopt</span><span>(</span><span>$con</span><span>,</span><span> CURLOPT_TIMEOUT</span><span>,</span><span>(</span><span>int</span><span>)</span><span>$timeout</span><span>);</span><span>
		curl_setopt</span><span>(</span><span>$con</span><span>,</span><span> CURLOPT_SSL_VERIFYPEER</span><span>,</span><span>1</span><span>);</span><span>
		curl_setopt</span><span>(</span><span>$con</span><span>,</span><span> CURLOPT_SSL_VERIFYHOST</span><span>,</span><span>2</span><span>);</span><span>//curl_setopt($ch, CURLOPT_SSLCERT, $pemfile); </span><span>//curl_setopt($ch, CURLOPT_SSLCERTTYPE, 'PEM');</span><span>
		curl_setopt</span><span>(</span><span>$con</span><span>,</span><span> CURLOPT_CAINFO</span><span>,</span><span> $pemfile</span><span>);</span><span>return</span><span> curl_exec</span><span>(</span><span>$con</span><span>);</span><span>}</span><span>/**
	 * @desc 封装curl的调用接口,post Json数据的请求方式
	 */</span><span>function</span><span> doCurlPostJsonReq</span><span>(</span><span>$url</span><span>,</span><span> $requestString</span><span>,</span><span> $timeout </span><span>=</span><span>25</span><span>){</span><span>if</span><span>(</span><span>$url </span><span>==</span><span>""</span><span>||</span><span> $requestString </span><span>==</span><span>""</span><span>||</span><span> $timeout </span><span><=</span><span>0</span><span>){</span><span>return</span><span>false</span><span>;</span><span>}</span><span>
	    $con </span><span>=</span><span> curl_init</span><span>((</span><span>string</span><span>)</span><span>$url</span><span>);</span><span>
	    curl_setopt</span><span>(</span><span>$con</span><span>,</span><span> CURLOPT_HEADER</span><span>,</span><span>false</span><span>);</span><span>
	    curl_setopt</span><span>(</span><span>$con</span><span>,</span><span> CURLOPT_POSTFIELDS</span><span>,</span><span> $requestString</span><span>);</span><span>
	    curl_setopt</span><span>(</span><span>$con</span><span>,</span><span> CURLOPT_POST</span><span>,</span><span>true</span><span>);</span><span>
	    curl_setopt</span><span>(</span><span>$con</span><span>,</span><span> CURLOPT_RETURNTRANSFER</span><span>,</span><span>true</span><span>);</span><span>
	    curl_setopt</span><span>(</span><span>$con</span><span>,</span><span> CURLOPT_TIMEOUT</span><span>,</span><span>(</span><span>int</span><span>)</span><span>$timeout</span><span>);</span><span>		curl_setopt</span><span>(</span><span>$con</span><span>,</span><span> CURLOPT_HTTPHEADER</span><span>,</span><span> array</span><span>(</span><span>'Content-Type: application/json'</span><span>,</span><span>'Content-Length: '</span><span>.</span><span> strlen</span><span>(</span><span>$data_string</span><span>))</span><span>);</span><span>return</span><span> curl_exec</span><span>(</span><span>$con</span><span>);</span><span>}</span><span>/**
	 * @desc 封装curl的调用接口,post form数据的请求方式
	 */</span><span>function</span><span> doCurlPostFormReq</span><span>(</span><span>$url</span><span>,</span><span> $requestString</span><span>,</span><span> $timeout </span><span>=</span><span>25</span><span>){</span><span>if</span><span>(</span><span>$url </span><span>==</span><span>""</span><span>||</span><span> $requestString </span><span>==</span><span>""</span><span>||</span><span> $timeout </span><span><=</span><span>0</span><span>){</span><span>return</span><span>false</span><span>;</span><span>}</span><span>
	    $con </span><span>=</span><span> curl_init</span><span>((</span><span>string</span><span>)</span><span>$url</span><span>);</span><span>
	    curl_setopt</span><span>(</span><span>$con</span><span>,</span><span> CURLOPT_HEADER</span><span>,</span><span>false</span><span>);</span><span>
	    curl_setopt</span><span>(</span><span>$con</span><span>,</span><span> CURLOPT_POSTFIELDS</span><span>,</span><span> $requestString</span><span>);</span><span>
	    curl_setopt</span><span>(</span><span>$con</span><span>,</span><span> CURLOPT_POST</span><span>,</span><span>true</span><span>);</span><span>
	    curl_setopt</span><span>(</span><span>$con</span><span>,</span><span> CURLOPT_RETURNTRANSFER</span><span>,</span><span>true</span><span>);</span><span>
	    curl_setopt</span><span>(</span><span>$con</span><span>,</span><span> CURLOPT_TIMEOUT</span><span>,</span><span>(</span><span>int</span><span>)</span><span>$timeout</span><span>);</span><span>		curl_setopt</span><span>(</span><span>$con</span><span>,</span><span> CURLOPT_HTTPHEADER</span><span>,</span><span> array</span><span>(</span><span>'Content-Type: application/x-www-form-urlencoded'</span><span>)</span><span>);</span><span>return</span><span> curl_exec</span><span>(</span><span>$con</span><span>);</span><span>}</span><span>/**
	 * @desc 封装curl的调用接口,get的请求方式
	 */</span><span>function</span><span> doCurlGetRequest</span><span>(</span><span>$url</span><span>,</span><span> $data </span><span>=</span><span> array</span><span>(),</span><span> $timeout </span><span>=</span><span>10</span><span>)</span><span>{</span><span>if</span><span>(</span><span>$url </span><span>==</span><span>""</span><span>||</span><span> $timeout </span><span><=</span><span>0</span><span>){</span><span>return</span><span>false</span><span>;</span><span>}</span><span>if</span><span>(</span><span>$data </span><span>!=</span><span> array</span><span>())</span><span>{</span><span>
			$url </span><span>=</span><span> $url </span><span>.</span><span>'?'</span><span>.</span><span> http_build_query</span><span>(</span><span>$data</span><span>);</span><span>}</span><span>Log</span><span>::</span><span>write</span><span>(</span><span>"发送URL["</span><span>.</span><span>$url</span><span>.</span><span>"]"</span><span>);</span><span>
		$con </span><span>=</span><span> curl_init</span><span>((</span><span>string</span><span>)</span><span>$url</span><span>);</span><span>
		curl_setopt</span><span>(</span><span>$con</span><span>,</span><span> CURLOPT_HEADER</span><span>,</span><span>false</span><span>);</span><span>
		curl_setopt</span><span>(</span><span>$con</span><span>,</span><span> CURLOPT_RETURNTRANSFER</span><span>,</span><span>true</span><span>);</span><span>
		curl_setopt</span><span>(</span><span>$con</span><span>,</span><span> CURLOPT_TIMEOUT</span><span>,</span><span>(</span><span>int</span><span>)</span><span>$timeout</span><span>);</span><span>
		curl_setopt</span><span>(</span><span>$con</span><span>,</span><span> CURLOPT_SSL_VERIFYPEER</span><span>,</span><span>false</span><span>);</span><span>
		curl_setopt</span><span>(</span><span>$con</span><span>,</span><span> CURLOPT_SSL_VERIFYHOST</span><span>,</span><span>false</span><span>);</span><span>return</span><span> curl_exec</span><span>(</span><span>$con</span><span>);</span><span>}</span><span>/**
	 *截取字符串,中文无乱码
	*/</span><span>function</span><span> msubstr</span><span>(</span><span>$str</span><span>,</span><span> $start</span><span>=</span><span>0</span><span>,</span><span> $length</span><span>=</span><span>0</span><span>,</span><span>$suffix</span><span>=</span><span>false</span><span>,</span><span>$charset</span><span>=</span><span>"utf-8"</span><span>){</span><span>if</span><span>(</span><span>mb_strlen</span><span>(</span><span>$str</span><span>,</span><span>"UTF8"</span><span>)</span><span>>=</span><span> $length</span><span>){</span><span>
	        $next_str </span><span>=</span><span>"…"</span><span>;</span><span>}</span><span>else</span><span>{</span><span>
	        $next_str </span><span>=</span><span>""</span><span>;</span><span>}</span><span>if</span><span>(</span><span>function_exists</span><span>(</span><span>"mb_substr"</span><span>)){</span><span>if</span><span>(</span><span>$suffix</span><span>)</span><span>// return mb_substr($str, $start, $length, $charset)."…";</span><span>return</span><span> mb_substr</span><span>(</span><span>$str</span><span>,</span><span> $start</span><span>,</span><span> $length</span><span>,</span><span> $charset</span><span>).</span><span>$next_str</span><span>;</span><span>else</span><span>return</span><span> mb_substr</span><span>(</span><span>$str</span><span>,</span><span> $start</span><span>,</span><span> $length</span><span>,</span><span> $charset</span><span>);</span><span>}</span><span>elseif</span><span>(</span><span>function_exists</span><span>(</span><span>'iconv_substr'</span><span>))</span><span>{</span><span>if</span><span>(</span><span>$suffix</span><span>)</span><span>// return iconv_substr($str,$start,$length,$charset)."…";</span><span>return</span><span> iconv_substr</span><span>(</span><span>$str</span><span>,</span><span>$start</span><span>,</span><span>$length</span><span>,</span><span>$charset</span><span>).</span><span>$next_str</span><span>;</span><span>else</span><span>return</span><span> iconv_substr</span><span>(</span><span>$str</span><span>,</span><span>$start</span><span>,</span><span>$length</span><span>,</span><span>$charset</span><span>);</span><span>}</span><span>
	    $re</span><span>[</span><span>'utf-8'</span><span>]</span><span>=</span><span>"/[x01-x7f]|[xc2-xdf][x80-xbf]|[xe0-xef][x80-xbf]{2}|[xf0-xff][x80-xbf]{3}/"</span><span>;</span><span>
	    $re</span><span>[</span><span>'gb2312'</span><span>]</span><span>=</span><span>"/[x01-x7f]|[xb0-xf7][xa0-xfe]/"</span><span>;</span><span>
	    $re</span><span>[</span><span>'gbk'</span><span>]</span><span>=</span><span>"/[x01-x7f]|[x81-xfe][x40-xfe]/"</span><span>;</span><span>
	    $re</span><span>[</span><span>'big5'</span><span>]</span><span>=</span><span>"/[x01-x7f]|[x81-xfe]([x40-x7e]|xa1-xfe])/"</span><span>;</span><span>
	    preg_match_all</span><span>(</span><span>$re</span><span>[</span><span>$charset</span><span>],</span><span> $str</span><span>,</span><span> $match</span><span>);</span><span>
	    $slice </span><span>=</span><span> join</span><span>(</span><span>""</span><span>,</span><span>array_slice</span><span>(</span><span>$match</span><span>[</span><span>0</span><span>],</span><span> $start</span><span>,</span><span> $length</span><span>));</span><span>if</span><span>(</span><span>$suffix</span><span>)</span><span>return</span><span> $slice</span><span>.</span><span>"…"</span><span>;</span><span>if</span><span>(!</span><span>$suffix</span><span>)</span><span>return</span><span> $slice</span><span>;</span><span>return</span><span> $slice</span><span>;</span><span>}</span><span>/**
	 *用户名*隐藏
	 *$str = "如来神掌";
	 *echo cut_str($str, 1, 0).'**'.cut_str($str, 1, -1);
	 *输出:如**掌
	*/</span><span>function</span><span> cut_str</span><span>(</span><span>$string</span><span>,</span><span> $sublen</span><span>,</span><span> $start </span><span>=</span><span>0</span><span>,</span><span> $code </span><span>=</span><span>'UTF-8'</span><span>)</span><span>{</span><span>if</span><span>(</span><span>$code </span><span>==</span><span>'UTF-8'</span><span>)</span><span>{</span><span>
	        $pa </span><span>=</span><span>"/[\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]/"</span><span>;</span><span>
	        preg_match_all</span><span>(</span><span>$pa</span><span>,</span><span> $string</span><span>,</span><span> $t_string</span><span>);</span><span>if</span><span>(</span><span>count</span><span>(</span><span>$t_string</span><span>[</span><span>0</span><span>])</span><span>-</span><span> $start </span><span>></span><span> $sublen</span><span>)</span><span>return</span><span> join</span><span>(</span><span>''</span><span>,</span><span> array_slice</span><span>(</span><span>$t_string</span><span>[</span><span>0</span><span>],</span><span> $start</span><span>,</span><span> $sublen</span><span>));</span><span>return</span><span> join</span><span>(</span><span>''</span><span>,</span><span> array_slice</span><span>(</span><span>$t_string</span><span>[</span><span>0</span><span>],</span><span> $start</span><span>,</span><span> $sublen</span><span>));</span><span>}</span><span>else</span><span>{</span><span>
	        $start </span><span>=</span><span> $start</span><span>*</span><span>2</span><span>;</span><span>
	        $sublen </span><span>=</span><span> $sublen</span><span>*</span><span>2</span><span>;</span><span>
	        $strlen </span><span>=</span><span> strlen</span><span>(</span><span>$string</span><span>);</span><span>
	        $tmpstr </span><span>=</span><span>''</span><span>;</span><span>for</span><span>(</span><span>$i</span><span>=</span><span>0</span><span>;</span><span> $i</span><span><</span><span> $strlen</span><span>;</span><span> $i</span><span>&#43;&#43;)</span><span>{</span><span>if</span><span>(</span><span>$i</span><span>>=</span><span>$start </span><span>&&</span><span> $i</span><span><</span><span>(</span><span>$start</span><span>&#43;</span><span>$sublen</span><span>))</span><span>{</span><span>if</span><span>(</span><span>ord</span><span>(</span><span>substr</span><span>(</span><span>$string</span><span>,</span><span> $i</span><span>,</span><span>1</span><span>))></span><span>129</span><span>)</span><span>{</span><span>
	                    $tmpstr</span><span>.=</span><span> substr</span><span>(</span><span>$string</span><span>,</span><span> $i</span><span>,</span><span>2</span><span>);</span><span>}</span><span>else</span><span>{</span><span>
	                    $tmpstr</span><span>.=</span><span> substr</span><span>(</span><span>$string</span><span>,</span><span> $i</span><span>,</span><span>1</span><span>);</span><span>}</span><span>}</span><span>if</span><span>(</span><span>ord</span><span>(</span><span>substr</span><span>(</span><span>$string</span><span>,</span><span> $i</span><span>,</span><span>1</span><span>))></span><span>129</span><span>)</span><span> $i</span><span>++;</span><span>}</span><span>//if(strlen($tmpstr)< $strlen ) $tmpstr.= "...";</span><span>return</span><span> $tmpstr</span><span>;</span><span>}</span><span>}</span><span>/*
	*解决json_encode转换中文的问题,在php5.4下json_encode本身解决的这个问题
	*/</span><span>function</span><span> unescapedUnicode</span><span>(</span><span>$match</span><span>)</span><span>{</span><span>return</span><span> mb_convert_encoding</span><span>(</span><span>pack</span><span>(</span><span>'H*'</span><span>,</span><span> $match</span><span>[</span><span>1</span><span>]),</span><span>'UTF-8'</span><span>,</span><span>'UCS-2BE'</span><span>);</span><span>}</span><span>/**
	*截取手机号
	*demo:13112345678
	*return:131****5678
	*/</span><span>function</span><span> hidtel</span><span>(</span><span>$phone</span><span>){</span><span>
	    $IsWhat </span><span>=</span><span> preg_match</span><span>(</span><span>'/(0[0-9]{2,3}[-]?[2-9][0-9]{6,7}[-]?[0-9]?)/i'</span><span>,</span><span>$phone</span><span>);</span><span>//固定电话</span><span>if</span><span>(</span><span>$IsWhat </span><span>==</span><span>1</span><span>){</span><span>return</span><span> preg_replace</span><span>(</span><span>'/(0[0-9]{2,3}[-]?[2-9])[0-9]{3,4}([0-9]{3}[-]?[0-9]?)/i'</span><span>,</span><span>'$1****$2'</span><span>,</span><span>$phone</span><span>);</span><span>}</span><span>else</span><span>{</span><span>return</span><span>  preg_replace</span><span>(</span><span>'/(1[358]{1}[0-9])[0-9]{4}([0-9]{4})/i'</span><span>,</span><span>'$1****$2'</span><span>,</span><span>$phone</span><span>);</span><span>}</span><span>}</span><span>//获取IP</span><span>function</span><span> getIp</span><span>()</span><span>{</span><span>if</span><span>(</span><span>isset</span><span>(</span><span>$_SERVER</span><span>)){</span><span>if</span><span>(</span><span>isset</span><span>(</span><span>$_SERVER</span><span>[</span><span>"HTTP_X_FORWARDED_FOR"</span><span>])){</span><span>
				$realip </span><span>=</span><span> $_SERVER</span><span>[</span><span>"HTTP_X_FORWARDED_FOR"</span><span>];</span><span>}</span><span>else</span><span>if</span><span>(</span><span>isset</span><span>(</span><span>$_SERVER</span><span>[</span><span>"HTTP_CLIENT_IP"</span><span>]))</span><span>{</span><span>
				$realip </span><span>=</span><span> $_SERVER</span><span>[</span><span>"HTTP_CLIENT_IP"</span><span>];</span><span>}</span><span>else</span><span>{</span><span>
				$realip </span><span>=</span><span> $_SERVER</span><span>[</span><span>"REMOTE_ADDR"</span><span>];</span><span>}</span><span>}</span><span>else</span><span>{</span><span>if</span><span>(</span><span>getenv</span><span>(</span><span>"HTTP_X_FORWARDED_FOR"</span><span>)){</span><span>
				$realip </span><span>=</span><span> getenv</span><span>(</span><span>"HTTP_X_FORWARDED_FOR"</span><span>);</span><span>}</span><span>else</span><span>if</span><span>(</span><span>getenv</span><span>(</span><span>"HTTP_CLIENT_IP"</span><span>))</span><span>{</span><span>
				$realip </span><span>=</span><span> getenv</span><span>(</span><span>"HTTP_CLIENT_IP"</span><span>);</span><span>}</span><span>else</span><span>{</span><span>
				$realip </span><span>=</span><span> getenv</span><span>(</span><span>"REMOTE_ADDR"</span><span>);</span><span>}</span><span>}</span><span>return</span><span> $realip</span><span>;</span><span>}</span><span>//把汉字转换为数字</span><span>function</span><span> getUnicodeFromOneUTF8</span><span>(</span><span>$word</span><span>)</span><span>{</span><span>//获取其字符的内部数组表示,所以本文件应用utf-8编码!  </span><span>//$word = iconv("gb2312", "utf-8", $word);</span><span>if</span><span>(</span><span>is_array</span><span>(</span><span>$word</span><span>))</span><span>			$arr </span><span>=</span><span> $word</span><span>;</span><span>else</span><span>			$arr </span><span>=</span><span> str_split</span><span>(</span><span>$word</span><span>);</span><span>//此时,$arr应类似array(228, 189, 160)  </span><span>//定义一个空字符串存储  </span><span>
		$bin_str </span><span>=</span><span>''</span><span>;</span><span>//转成数字再转成二进制字符串,最后联合起来。  </span><span>foreach</span><span>(</span><span>$arr </span><span>as</span><span> $value</span><span>)</span><span>			$bin_str </span><span>.=</span><span> decbin</span><span>(</span><span>ord</span><span>(</span><span>$value</span><span>));</span><span>//此时,$bin_str应类似111001001011110110100000</span><span>//正则截取  </span><span>
		$bin_str </span><span>=</span><span> preg_replace</span><span>(</span><span>'/^.{4}(.{4}).{2}(.{6}).{2}(.{6})$/'</span><span>,</span><span>'$1$2$3'</span><span>,</span><span> $bin_str</span><span>);</span><span>//此时, $bin_str应类似0100111101100000,如果是汉字"你"</span><span>return</span><span> bindec</span><span>(</span><span>$bin_str</span><span>);</span><span>//返回类似20320, 汉字"你"  </span><span>//return dechex(bindec($bin_str));  </span><span>//如想返回十六进制4f60,用这句  </span><p><span>}</span></p><p><span>网站访问地址:http://www.5k5c.com/blog/179</span></p>

以上就介绍了 PHP封装curl的调用接口及常用函数,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。