php 中curl请求模块封装
<?php
namespace App\Utils;
/**
* http 工具类
* @author Administrator
*
*/
class HttpUtils
{
private static $_instance;
private function __construct()
{
}
public static function getInstance()
{
if( null == self::$_instance )
{
self::$_instance = new HttpUtils();
}
return self::$_instance;
}
/**
* http curl 请求
* @param unknown $remote
* @param unknown $method
* @param array $data
* @param array $headers
* @return string
*/
public function curl( $remote, $method = 'GET', $data = [], $headers = [], $format = 'STRING' )
{
if( !$remote )
{
$arrMsg[ 'code' ] = 0;
$arrMsg[ 'msg' ] = 'ERROR: undefined request url';
return $arrMsg;
}
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $remote );
curl_setopt( $ch, CURLOPT_HEADER, false );
curl_setopt( $ch, CURLOPT_TIMEOUT, 60 );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, false );
curl_setopt( $ch, CURLOPT_USERAGENT, $this->_getAgent() );
if( isset( $headers ) && !empty( $headers ) )
{
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
}
switch ( $method )
{
case 'GET':
break;
case 'PUT':
curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, 'PUT' );
switch ( $format )
{
case 'STRING':
curl_setopt( $ch, CURLOPT_POSTFIELDS, http_build_query( $data, '', '&' ) ); //设置请求体,提交数据包
break;
case 'JSON':
curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $data ) ); //设置请求体,提交数据包
break;
}
break;
case 'POST':
curl_setopt( $ch, CURLOPT_POST, 1 );
switch ( $format )
{
case 'STRING':
curl_setopt( $ch, CURLOPT_POSTFIELDS, http_build_query( $data, '', '&' ) );
break;
case 'JSON':
curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $data ) );
break;
}
break;
case 'DELETE':
curl_setopt ( $ch, CURLOPT_NOSIGNAL, true );
curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, 'DELETE' );
break;
}
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
$result = curl_exec( $ch );
$error = curl_error( $ch );
$curl_info = curl_getinfo( $ch );
curl_close( $ch );
if( isset( $error ) && strlen( $error ) > 0 )
{
//异常
$arrMsg[ 'code' ] = 0;
$arrMsg[ 'request' ] = $remote;
$arrMsg[ 'msg' ] = ' Curl ERROR: curl error code is ' . $error;
return $arrMsg;
}
if( 400 <= $curl_info[ 'http_code' ] )
{
//异常
$arrMsg[ 'code' ] = 0;
$arrMsg[ 'http_code' ] = $curl_info[ 'http_code' ];
$arrMsg[ 'request' ] = $remote;
$arrMsg[ 'errmsg' ] = $result;
$arrMsg[ 'msg' ] = ' CURL ERROR: http response error ';
return $arrMsg;
}
$arrMsg[ 'code' ] = $curl_info[ 'http_code' ];
$arrMsg[ 'request' ] = $remote;
$arrMsg[ 'data' ] = $result;
$arrMsg[ 'msg' ] = 'REMOTE: http code ' . $curl_info[ 'http_code' ];
return $arrMsg;
}
/**
* 获取远程数据是否正常
* @param unknown $remote
* @return string
*/
public function curlGet( $remote, $headers = [], $proxy = false )
{
if( !$remote )
{
$arrMsg[ 'code' ] = 0;
$arrMsg[ 'msg' ] = 'ERROR: undefined request url';
return $arrMsg;
}
$curl = curl_init();
curl_setopt( $curl, CURLOPT_URL, $remote );
curl_setopt( $curl, CURLOPT_HEADER, false );
curl_setopt( $curl, CURLOPT_TIMEOUT, 75 );
curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $curl, CURLOPT_FOLLOWLOCATION, true );
curl_setopt( $curl, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $curl, CURLOPT_SSL_VERIFYHOST, false );
// 可配置随机取 USERAGENT
$strUserAgent = $this->_getAgent();
curl_setopt( $curl, CURLOPT_USERAGENT, $strUserAgent );
curl_setopt( $curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1 );
if( !empty( $headers ) )
{
curl_setopt( $curl, CURLOPT_HTTPHEADER, $headers );
}
if( $proxy )
{
// 配置多代理模式, 可进行随机切换或者指定模式切换使用代理
$arrAgentINfo = $this->_getProxy();
curl_setopt( $curl, CURLOPT_HTTPPROXYTUNNEL, false );
curl_setopt( $curl, CURLOPT_PROXYTYPE, CURLPROXY_HTTP );
curl_setopt( $curl, CURLOPT_PROXYAUTH, CURLAUTH_BASIC );
// 设置代理服务器
curl_setopt( $curl, CURLOPT_PROXY, $arrAgentINfo[ 'server' ] );
// 设置隧道验证信息
curl_setopt( $curl, CURLOPT_PROXYUSERPWD, "{$arrAgentINfo['user']}:{$arrAgentINfo['passwd']}");
}
$iStart = microtime( true );
try{
$data = curl_exec( $curl );
$error_code = curl_errno( $curl );
$error_info = curl_error( $curl );
$curl_info = curl_getinfo( $curl );
curl_close( $curl );
$iUserSec = sprintf( '%0.2f', microtime( true ) - $iStart );
if( isset( $error_info ) && strlen( $error_info ) > 0 )
{
$arrMsg[ 'code' ] = 0;
$arrMsg[ 'curl_code' ]= $error_code;
$arrMsg[ 'request' ]= $remote;
$arrMsg[ 'msg' ] = 'CURL ERROR: '. $error_info;
$arrMsg[ 'time' ] = $iUserSec;
return $arrMsg;
}
$arrMsg[ 'code' ] = $curl_info[ 'http_code' ];
$arrMsg[ 'request' ] = $remote;
$arrMsg[ 'data' ] = $data;
$arrMsg[ 'msg' ] = 'REMOTE: http code ' . $curl_info[ 'http_code' ];
$arrMsg[ 'time' ] = $iUserSec;
}catch ( \Exception $e ){
$arrMsg[ 'code' ] = 0;
$arrMsg[ 'request' ] = $remote;
$arrMsg[ 'msg' ] = 'CURL ERROR: '. $e->getMessage();
$arrMsg[ 'time' ] = $iUserSec;
}
return $arrMsg;
}
/**
* 模拟post提交
* @param unknown $remoteUrl
* @param unknown $data
* @return boolean|string
*/
public function curlPost( $remoteUrl, $data, $headers = [], $format = 'STRING' )
{
if( !$remoteUrl || !$data || empty( $data ) )
{
return false;
}
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $remoteUrl );
curl_setopt( $ch, CURLOPT_POST, true );
switch ( $format )
{
case 'STRING':
curl_setopt( $ch, CURLOPT_POSTFIELDS, http_build_query( $data, '', '&' ) );
break;
case 'JSON':
curl_setopt( $ch, CURLOPT_POSTFIELDS, \GuzzleHttp\json_encode( $data ) );
break;
}
curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, 20 );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_TIMEOUT, 180 );
if( !empty( $headers ) )
{
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
}
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, false );
$result = curl_exec( $ch );
$error = curl_error( $ch );
$curl_info = curl_getinfo( $ch );
curl_close( $ch );
if( isset( $error ) && strlen( $error ) > 0 )
{
//异常
$arrMsg[ 'code' ] = 0;
$arrMsg[ 'request' ] = $remoteUrl;
$arrMsg[ 'msg' ] = ' Curl ERROR: curl error code is ' . $error;
return $arrMsg;
}
if( 200 != $curl_info[ 'http_code' ] )
{
//异常
$arrMsg[ 'code' ] = 0;
$arrMsg[ 'http_code' ] = $curl_info[ 'http_code' ];
$arrMsg[ 'request' ] = $remoteUrl;
$arrMsg[ 'data' ] = $result;
$arrMsg[ 'msg' ] = ' CURL ERROR: http response error ';
return $arrMsg;
}
$arrMsg[ 'code' ] = $curl_info[ 'http_code' ];
$arrMsg[ 'request' ] = $remoteUrl;
$arrMsg[ 'data' ] = $result;
$arrMsg[ 'msg' ] = 'REMOTE: http code ' . $curl_info[ 'http_code' ];
return $arrMsg;
}
/**
* 模拟post提交
* @param unknown $remoteUrl
* @param unknown $data
* @return boolean|string
*/
public function curlPut( $remoteUrl, $data, $headers = [], $format = 'STRING' )
{
if( !$remoteUrl || !$data || empty( $data ) )
{
return false;
}
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $remoteUrl );
curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, 'PUT' );
switch ( $format )
{
case 'STRING':
curl_setopt( $ch, CURLOPT_POSTFIELDS, http_build_query( $data, '', '&' ) );
break;
case 'JSON':
curl_setopt( $ch, CURLOPT_POSTFIELDS, \GuzzleHttp\json_encode( $data ) );
break;
}
curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, 0 );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_TIMEOUT, 180 );
curl_setopt( $ch, CURLOPT_USERAGENT, $this->_getAgent() );
if( !empty( $headers ) )
{
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
}
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, false );
$result = curl_exec( $ch );
$error = curl_error( $ch );
$curl_info = curl_getinfo( $ch );
curl_close( $ch );
if( isset( $error ) && strlen( $error ) > 0 )
{
//异常
$arrMsg[ 'code' ] = 0;
$arrMsg[ 'request' ] = $remoteUrl;
$arrMsg[ 'msg' ] = ' Curl ERROR: curl error code is ' . $error;
return $arrMsg;
}
if( 200 != $curl_info[ 'http_code' ] )
{
//异常
$arrMsg[ 'code' ] = 0;
$arrMsg[ 'http_code' ] = $curl_info[ 'http_code' ];
$arrMsg[ 'request' ] = $remoteUrl;
$arrMsg[ 'data' ] = $result;
$arrMsg[ 'msg' ] = ' CURL ERROR: http response error ';
return $arrMsg;
}
$arrMsg[ 'code' ] = $curl_info[ 'http_code' ];
$arrMsg[ 'request' ] = $remoteUrl;
$arrMsg[ 'data' ] = $result;
$arrMsg[ 'msg' ] = 'REMOTE: http code ' . $curl_info[ 'http_code' ];
return $arrMsg;
}
/**
* 模拟post提交
* @param unknown $remoteUrl
* @param unknown $data
* @return boolean|string
*/
public function curlDelete( $remoteUrl, $headers = [] )
{
if( !$remoteUrl )
{
return false;
}
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $remoteUrl );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_TIMEOUT, 60 );
curl_setopt( $ch, CURLOPT_USERAGENT, $this->_getAgent() );
if( !empty( $headers ) )
{
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
}
curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, 'DELETE' );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, false );
$result = curl_exec( $ch );
$error = curl_error( $ch );
$curl_info = curl_getinfo( $ch );
curl_close( $ch );
if( isset( $error ) && strlen( $error ) > 0 )
{
//异常
$arrMsg[ 'code' ] = 0;
$arrMsg[ 'request' ] = $remoteUrl;
$arrMsg[ 'msg' ] = ' Curl ERROR: curl error code is ' . $error;
return $arrMsg;
}
if( 200 <= $curl_info[ 'http_code' ] && 400 > $curl_info[ 'http_code' ])
{
//异常
$arrMsg[ 'code' ] = 0;
$arrMsg[ 'http_code' ] = $curl_info[ 'http_code' ];
$arrMsg[ 'request' ] = $remoteUrl;
$arrMsg[ 'msg' ] = ' CURL ERROR: http response error ';
return $arrMsg;
}
$arrMsg[ 'code' ] = $curl_info[ 'http_code' ];
$arrMsg[ 'request' ] = $remoteUrl;
$arrMsg[ 'data' ] = $result;
$arrMsg[ 'msg' ] = 'REMOTE: http code ' . $curl_info[ 'http_code' ];
return $arrMsg;
}
/**
* 随机取 代理信息
* @return unknown[]
*/
private function _getProxy()
{
$arrRet = array();
$arrCacheProxy = config( 'agents.proxys' );
$proxy = array_shift( $arrCacheProxy );
$arrRet[ 'name' ] = $proxy[ 'name' ];
$arrRet[ 'server' ] = $proxy[ 'proxy' ] . ':' . $proxy[ 'port' ];
if( isset( $proxy[ 'user' ] ) && $proxy[ 'user' ] )
{
$arrRet[ 'user' ] = $proxy[ 'user' ];
}
if( isset( $proxy[ 'passwd' ] ) && $proxy[ 'passwd' ] )
{
$arrRet[ 'passwd' ] = $proxy[ 'passwd' ];
}
return $arrRet;
}
/**
* 随机获取 user-agent
*/
private function _getAgent()
{
$agent = config( 'agents.agents' );
$iGNum = count( $agent );
$iCurrent = rand( 0, intval( $iGNum - 1 ) );
return $agent[$iCurrent];
}
/**
* 模拟post提交
* @param unknown $remoteUrl
* @param unknown $data
* @return boolean|string
*/
public function curlPostWithCookie( $remoteUrl, $data,$cookie, $headers = [], $format = 'STRING' )
{
if( !$remoteUrl || !$data || empty( $data ) )
{
return false;
}
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $remoteUrl );
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie ); //设置Cookie信息保存在指定的文件中
switch ( $format )
{
case 'STRING':
curl_setopt( $ch, CURLOPT_POSTFIELDS, http_build_query( $data, '', '&' ) );
break;
case 'JSON':
curl_setopt( $ch, CURLOPT_POSTFIELDS, \GuzzleHttp\json_encode( $data ) );
break;
}
curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, 0 );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_TIMEOUT, 240 );
if( !empty( $headers ) )
{
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
}
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, false );
$result = curl_exec( $ch );
$error = curl_error( $ch );
$curl_info = curl_getinfo( $ch );
curl_close( $ch );
if( isset( $error ) && strlen( $error ) > 0 )
{
//异常
$arrMsg[ 'code' ] = 0;
$arrMsg[ 'request' ] = $remoteUrl;
$arrMsg[ 'msg' ] = ' Curl ERROR: curl error code is ' . $error;
return $arrMsg;
}
if( 200 != $curl_info[ 'http_code' ] )
{
//异常
$arrMsg[ 'code' ] = 0;
$arrMsg[ 'http_code' ] = $curl_info[ 'http_code' ];
$arrMsg[ 'request' ] = $remoteUrl;
$arrMsg[ 'data' ] = $result;
$arrMsg[ 'msg' ] = ' CURL ERROR: http response error ';
return $arrMsg;
}
$arrMsg[ 'code' ] = $curl_info[ 'http_code' ];
$arrMsg[ 'request' ] = $remoteUrl;
$arrMsg[ 'data' ] = $result;
$arrMsg[ 'msg' ] = 'REMOTE: http code ' . $curl_info[ 'http_code' ];
return $arrMsg;
}
/**
* 获取链接详细信息
* @author Administrator
* @datetime 2019年5月7日 下午5:39:16
* @comment
*
* @param unknown $url
* @return array|string[]|unknown[]|mixed[]
*/
public function curlGetHeader( $url )
{
$arrRet = array();
if( !$url || strlen( $url ) <= 0 )
{
return $arrRet;
}
if( !starts_with( $url , [ 'http', 'https', 'Http', 'Https', 'HTTP', 'HTTPS' ] ) )
{
return $arrRet;
}
try{
$header = get_headers( $url, TRUE );
if ( strpos( $header[0], 302 ) || strpos( $header[0], 301 ) )
{
if( isset( $header[ 'Location' ] ) && is_array( $header[ 'Location' ] ) && !empty( $header[ 'Location' ] ) )
{
$url = $header[ 'Location' ][count( $header['Location'] )-1 ];
}
else if( isset( $header[ 'Location' ] ) )
{
$url = $header[ 'Location' ];
}
}
$urlInfo = parse_url( $url );
if( isset( $urlInfo ) && !empty( $urlInfo ) )
{
$scheme = strtolower( $urlInfo[ 'scheme' ] );
$arrRet[ 'scheme' ] = $scheme;
$arrRet[ 'host' ] = strtolower( $urlInfo[ 'host' ] );
$arrRet[ 'url' ] = $scheme . '://' . $urlInfo[ 'host' ];
}
}catch( \Exception $e ){
if( preg_match( "/ssl3_get_server_certificate/i" , $e->getMessage() ) )
{
return $this->curlGetHeader( str_replace( 'https://' , 'http://', $url ) );
}
}
return $arrRet;
}
}