• PHP Curl请求封装


    php 中curl请求模块封装

    1. namespace App\Utils;
    2. /**
    3. * http 工具类
    4. * @author Administrator
    5. *
    6. */
    7. class HttpUtils
    8. {
    9. private static $_instance;
    10. private function __construct()
    11. {
    12. }
    13. public static function getInstance()
    14. {
    15. if( null == self::$_instance )
    16. {
    17. self::$_instance = new HttpUtils();
    18. }
    19. return self::$_instance;
    20. }
    21. /**
    22. * http curl 请求
    23. * @param unknown $remote
    24. * @param unknown $method
    25. * @param array $data
    26. * @param array $headers
    27. * @return string
    28. */
    29. public function curl( $remote, $method = 'GET', $data = [], $headers = [], $format = 'STRING' )
    30. {
    31. if( !$remote )
    32. {
    33. $arrMsg[ 'code' ] = 0;
    34. $arrMsg[ 'msg' ] = 'ERROR: undefined request url';
    35. return $arrMsg;
    36. }
    37. $ch = curl_init();
    38. curl_setopt( $ch, CURLOPT_URL, $remote );
    39. curl_setopt( $ch, CURLOPT_HEADER, false );
    40. curl_setopt( $ch, CURLOPT_TIMEOUT, 60 );
    41. curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
    42. curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
    43. curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, false );
    44. curl_setopt( $ch, CURLOPT_USERAGENT, $this->_getAgent() );
    45. if( isset( $headers ) && !empty( $headers ) )
    46. {
    47. curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
    48. }
    49. switch ( $method )
    50. {
    51. case 'GET':
    52. break;
    53. case 'PUT':
    54. curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, 'PUT' );
    55. switch ( $format )
    56. {
    57. case 'STRING':
    58. curl_setopt( $ch, CURLOPT_POSTFIELDS, http_build_query( $data, '', '&' ) ); //设置请求体,提交数据包
    59. break;
    60. case 'JSON':
    61. curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $data ) ); //设置请求体,提交数据包
    62. break;
    63. }
    64. break;
    65. case 'POST':
    66. curl_setopt( $ch, CURLOPT_POST, 1 );
    67. switch ( $format )
    68. {
    69. case 'STRING':
    70. curl_setopt( $ch, CURLOPT_POSTFIELDS, http_build_query( $data, '', '&' ) );
    71. break;
    72. case 'JSON':
    73. curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $data ) );
    74. break;
    75. }
    76. break;
    77. case 'DELETE':
    78. curl_setopt ( $ch, CURLOPT_NOSIGNAL, true );
    79. curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, 'DELETE' );
    80. break;
    81. }
    82. curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
    83. $result = curl_exec( $ch );
    84. $error = curl_error( $ch );
    85. $curl_info = curl_getinfo( $ch );
    86. curl_close( $ch );
    87. if( isset( $error ) && strlen( $error ) > 0 )
    88. {
    89. //异常
    90. $arrMsg[ 'code' ] = 0;
    91. $arrMsg[ 'request' ] = $remote;
    92. $arrMsg[ 'msg' ] = ' Curl ERROR: curl error code is ' . $error;
    93. return $arrMsg;
    94. }
    95. if( 400 <= $curl_info[ 'http_code' ] )
    96. {
    97. //异常
    98. $arrMsg[ 'code' ] = 0;
    99. $arrMsg[ 'http_code' ] = $curl_info[ 'http_code' ];
    100. $arrMsg[ 'request' ] = $remote;
    101. $arrMsg[ 'errmsg' ] = $result;
    102. $arrMsg[ 'msg' ] = ' CURL ERROR: http response error ';
    103. return $arrMsg;
    104. }
    105. $arrMsg[ 'code' ] = $curl_info[ 'http_code' ];
    106. $arrMsg[ 'request' ] = $remote;
    107. $arrMsg[ 'data' ] = $result;
    108. $arrMsg[ 'msg' ] = 'REMOTE: http code ' . $curl_info[ 'http_code' ];
    109. return $arrMsg;
    110. }
    111. /**
    112. * 获取远程数据是否正常
    113. * @param unknown $remote
    114. * @return string
    115. */
    116. public function curlGet( $remote, $headers = [], $proxy = false )
    117. {
    118. if( !$remote )
    119. {
    120. $arrMsg[ 'code' ] = 0;
    121. $arrMsg[ 'msg' ] = 'ERROR: undefined request url';
    122. return $arrMsg;
    123. }
    124. $curl = curl_init();
    125. curl_setopt( $curl, CURLOPT_URL, $remote );
    126. curl_setopt( $curl, CURLOPT_HEADER, false );
    127. curl_setopt( $curl, CURLOPT_TIMEOUT, 75 );
    128. curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true );
    129. curl_setopt( $curl, CURLOPT_FOLLOWLOCATION, true );
    130. curl_setopt( $curl, CURLOPT_SSL_VERIFYPEER, false );
    131. curl_setopt( $curl, CURLOPT_SSL_VERIFYHOST, false );
    132. // 可配置随机取 USERAGENT
    133. $strUserAgent = $this->_getAgent();
    134. curl_setopt( $curl, CURLOPT_USERAGENT, $strUserAgent );
    135. curl_setopt( $curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1 );
    136. if( !empty( $headers ) )
    137. {
    138. curl_setopt( $curl, CURLOPT_HTTPHEADER, $headers );
    139. }
    140. if( $proxy )
    141. {
    142. // 配置多代理模式, 可进行随机切换或者指定模式切换使用代理
    143. $arrAgentINfo = $this->_getProxy();
    144. curl_setopt( $curl, CURLOPT_HTTPPROXYTUNNEL, false );
    145. curl_setopt( $curl, CURLOPT_PROXYTYPE, CURLPROXY_HTTP );
    146. curl_setopt( $curl, CURLOPT_PROXYAUTH, CURLAUTH_BASIC );
    147. // 设置代理服务器
    148. curl_setopt( $curl, CURLOPT_PROXY, $arrAgentINfo[ 'server' ] );
    149. // 设置隧道验证信息
    150. curl_setopt( $curl, CURLOPT_PROXYUSERPWD, "{$arrAgentINfo['user']}:{$arrAgentINfo['passwd']}");
    151. }
    152. $iStart = microtime( true );
    153. try{
    154. $data = curl_exec( $curl );
    155. $error_code = curl_errno( $curl );
    156. $error_info = curl_error( $curl );
    157. $curl_info = curl_getinfo( $curl );
    158. curl_close( $curl );
    159. $iUserSec = sprintf( '%0.2f', microtime( true ) - $iStart );
    160. if( isset( $error_info ) && strlen( $error_info ) > 0 )
    161. {
    162. $arrMsg[ 'code' ] = 0;
    163. $arrMsg[ 'curl_code' ]= $error_code;
    164. $arrMsg[ 'request' ]= $remote;
    165. $arrMsg[ 'msg' ] = 'CURL ERROR: '. $error_info;
    166. $arrMsg[ 'time' ] = $iUserSec;
    167. return $arrMsg;
    168. }
    169. $arrMsg[ 'code' ] = $curl_info[ 'http_code' ];
    170. $arrMsg[ 'request' ] = $remote;
    171. $arrMsg[ 'data' ] = $data;
    172. $arrMsg[ 'msg' ] = 'REMOTE: http code ' . $curl_info[ 'http_code' ];
    173. $arrMsg[ 'time' ] = $iUserSec;
    174. }catch ( \Exception $e ){
    175. $arrMsg[ 'code' ] = 0;
    176. $arrMsg[ 'request' ] = $remote;
    177. $arrMsg[ 'msg' ] = 'CURL ERROR: '. $e->getMessage();
    178. $arrMsg[ 'time' ] = $iUserSec;
    179. }
    180. return $arrMsg;
    181. }
    182. /**
    183. * 模拟post提交
    184. * @param unknown $remoteUrl
    185. * @param unknown $data
    186. * @return boolean|string
    187. */
    188. public function curlPost( $remoteUrl, $data, $headers = [], $format = 'STRING' )
    189. {
    190. if( !$remoteUrl || !$data || empty( $data ) )
    191. {
    192. return false;
    193. }
    194. $ch = curl_init();
    195. curl_setopt( $ch, CURLOPT_URL, $remoteUrl );
    196. curl_setopt( $ch, CURLOPT_POST, true );
    197. switch ( $format )
    198. {
    199. case 'STRING':
    200. curl_setopt( $ch, CURLOPT_POSTFIELDS, http_build_query( $data, '', '&' ) );
    201. break;
    202. case 'JSON':
    203. curl_setopt( $ch, CURLOPT_POSTFIELDS, \GuzzleHttp\json_encode( $data ) );
    204. break;
    205. }
    206. curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, 20 );
    207. curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
    208. curl_setopt( $ch, CURLOPT_TIMEOUT, 180 );
    209. if( !empty( $headers ) )
    210. {
    211. curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
    212. }
    213. curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
    214. curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
    215. curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, false );
    216. $result = curl_exec( $ch );
    217. $error = curl_error( $ch );
    218. $curl_info = curl_getinfo( $ch );
    219. curl_close( $ch );
    220. if( isset( $error ) && strlen( $error ) > 0 )
    221. {
    222. //异常
    223. $arrMsg[ 'code' ] = 0;
    224. $arrMsg[ 'request' ] = $remoteUrl;
    225. $arrMsg[ 'msg' ] = ' Curl ERROR: curl error code is ' . $error;
    226. return $arrMsg;
    227. }
    228. if( 200 != $curl_info[ 'http_code' ] )
    229. {
    230. //异常
    231. $arrMsg[ 'code' ] = 0;
    232. $arrMsg[ 'http_code' ] = $curl_info[ 'http_code' ];
    233. $arrMsg[ 'request' ] = $remoteUrl;
    234. $arrMsg[ 'data' ] = $result;
    235. $arrMsg[ 'msg' ] = ' CURL ERROR: http response error ';
    236. return $arrMsg;
    237. }
    238. $arrMsg[ 'code' ] = $curl_info[ 'http_code' ];
    239. $arrMsg[ 'request' ] = $remoteUrl;
    240. $arrMsg[ 'data' ] = $result;
    241. $arrMsg[ 'msg' ] = 'REMOTE: http code ' . $curl_info[ 'http_code' ];
    242. return $arrMsg;
    243. }
    244. /**
    245. * 模拟post提交
    246. * @param unknown $remoteUrl
    247. * @param unknown $data
    248. * @return boolean|string
    249. */
    250. public function curlPut( $remoteUrl, $data, $headers = [], $format = 'STRING' )
    251. {
    252. if( !$remoteUrl || !$data || empty( $data ) )
    253. {
    254. return false;
    255. }
    256. $ch = curl_init();
    257. curl_setopt( $ch, CURLOPT_URL, $remoteUrl );
    258. curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, 'PUT' );
    259. switch ( $format )
    260. {
    261. case 'STRING':
    262. curl_setopt( $ch, CURLOPT_POSTFIELDS, http_build_query( $data, '', '&' ) );
    263. break;
    264. case 'JSON':
    265. curl_setopt( $ch, CURLOPT_POSTFIELDS, \GuzzleHttp\json_encode( $data ) );
    266. break;
    267. }
    268. curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, 0 );
    269. curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
    270. curl_setopt( $ch, CURLOPT_TIMEOUT, 180 );
    271. curl_setopt( $ch, CURLOPT_USERAGENT, $this->_getAgent() );
    272. if( !empty( $headers ) )
    273. {
    274. curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
    275. }
    276. curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
    277. curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, false );
    278. $result = curl_exec( $ch );
    279. $error = curl_error( $ch );
    280. $curl_info = curl_getinfo( $ch );
    281. curl_close( $ch );
    282. if( isset( $error ) && strlen( $error ) > 0 )
    283. {
    284. //异常
    285. $arrMsg[ 'code' ] = 0;
    286. $arrMsg[ 'request' ] = $remoteUrl;
    287. $arrMsg[ 'msg' ] = ' Curl ERROR: curl error code is ' . $error;
    288. return $arrMsg;
    289. }
    290. if( 200 != $curl_info[ 'http_code' ] )
    291. {
    292. //异常
    293. $arrMsg[ 'code' ] = 0;
    294. $arrMsg[ 'http_code' ] = $curl_info[ 'http_code' ];
    295. $arrMsg[ 'request' ] = $remoteUrl;
    296. $arrMsg[ 'data' ] = $result;
    297. $arrMsg[ 'msg' ] = ' CURL ERROR: http response error ';
    298. return $arrMsg;
    299. }
    300. $arrMsg[ 'code' ] = $curl_info[ 'http_code' ];
    301. $arrMsg[ 'request' ] = $remoteUrl;
    302. $arrMsg[ 'data' ] = $result;
    303. $arrMsg[ 'msg' ] = 'REMOTE: http code ' . $curl_info[ 'http_code' ];
    304. return $arrMsg;
    305. }
    306. /**
    307. * 模拟post提交
    308. * @param unknown $remoteUrl
    309. * @param unknown $data
    310. * @return boolean|string
    311. */
    312. public function curlDelete( $remoteUrl, $headers = [] )
    313. {
    314. if( !$remoteUrl )
    315. {
    316. return false;
    317. }
    318. $ch = curl_init();
    319. curl_setopt( $ch, CURLOPT_URL, $remoteUrl );
    320. curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
    321. curl_setopt( $ch, CURLOPT_TIMEOUT, 60 );
    322. curl_setopt( $ch, CURLOPT_USERAGENT, $this->_getAgent() );
    323. if( !empty( $headers ) )
    324. {
    325. curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
    326. }
    327. curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, 'DELETE' );
    328. curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
    329. curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, false );
    330. $result = curl_exec( $ch );
    331. $error = curl_error( $ch );
    332. $curl_info = curl_getinfo( $ch );
    333. curl_close( $ch );
    334. if( isset( $error ) && strlen( $error ) > 0 )
    335. {
    336. //异常
    337. $arrMsg[ 'code' ] = 0;
    338. $arrMsg[ 'request' ] = $remoteUrl;
    339. $arrMsg[ 'msg' ] = ' Curl ERROR: curl error code is ' . $error;
    340. return $arrMsg;
    341. }
    342. if( 200 <= $curl_info[ 'http_code' ] && 400 > $curl_info[ 'http_code' ])
    343. {
    344. //异常
    345. $arrMsg[ 'code' ] = 0;
    346. $arrMsg[ 'http_code' ] = $curl_info[ 'http_code' ];
    347. $arrMsg[ 'request' ] = $remoteUrl;
    348. $arrMsg[ 'msg' ] = ' CURL ERROR: http response error ';
    349. return $arrMsg;
    350. }
    351. $arrMsg[ 'code' ] = $curl_info[ 'http_code' ];
    352. $arrMsg[ 'request' ] = $remoteUrl;
    353. $arrMsg[ 'data' ] = $result;
    354. $arrMsg[ 'msg' ] = 'REMOTE: http code ' . $curl_info[ 'http_code' ];
    355. return $arrMsg;
    356. }
    357. /**
    358. * 随机取 代理信息
    359. * @return unknown[]
    360. */
    361. private function _getProxy()
    362. {
    363. $arrRet = array();
    364. $arrCacheProxy = config( 'agents.proxys' );
    365. $proxy = array_shift( $arrCacheProxy );
    366. $arrRet[ 'name' ] = $proxy[ 'name' ];
    367. $arrRet[ 'server' ] = $proxy[ 'proxy' ] . ':' . $proxy[ 'port' ];
    368. if( isset( $proxy[ 'user' ] ) && $proxy[ 'user' ] )
    369. {
    370. $arrRet[ 'user' ] = $proxy[ 'user' ];
    371. }
    372. if( isset( $proxy[ 'passwd' ] ) && $proxy[ 'passwd' ] )
    373. {
    374. $arrRet[ 'passwd' ] = $proxy[ 'passwd' ];
    375. }
    376. return $arrRet;
    377. }
    378. /**
    379. * 随机获取 user-agent
    380. */
    381. private function _getAgent()
    382. {
    383. $agent = config( 'agents.agents' );
    384. $iGNum = count( $agent );
    385. $iCurrent = rand( 0, intval( $iGNum - 1 ) );
    386. return $agent[$iCurrent];
    387. }
    388. /**
    389. * 模拟post提交
    390. * @param unknown $remoteUrl
    391. * @param unknown $data
    392. * @return boolean|string
    393. */
    394. public function curlPostWithCookie( $remoteUrl, $data,$cookie, $headers = [], $format = 'STRING' )
    395. {
    396. if( !$remoteUrl || !$data || empty( $data ) )
    397. {
    398. return false;
    399. }
    400. $ch = curl_init();
    401. curl_setopt( $ch, CURLOPT_URL, $remoteUrl );
    402. curl_setopt( $ch, CURLOPT_POST, true );
    403. curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie ); //设置Cookie信息保存在指定的文件中
    404. switch ( $format )
    405. {
    406. case 'STRING':
    407. curl_setopt( $ch, CURLOPT_POSTFIELDS, http_build_query( $data, '', '&' ) );
    408. break;
    409. case 'JSON':
    410. curl_setopt( $ch, CURLOPT_POSTFIELDS, \GuzzleHttp\json_encode( $data ) );
    411. break;
    412. }
    413. curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, 0 );
    414. curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
    415. curl_setopt( $ch, CURLOPT_TIMEOUT, 240 );
    416. if( !empty( $headers ) )
    417. {
    418. curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
    419. }
    420. curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
    421. curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, false );
    422. $result = curl_exec( $ch );
    423. $error = curl_error( $ch );
    424. $curl_info = curl_getinfo( $ch );
    425. curl_close( $ch );
    426. if( isset( $error ) && strlen( $error ) > 0 )
    427. {
    428. //异常
    429. $arrMsg[ 'code' ] = 0;
    430. $arrMsg[ 'request' ] = $remoteUrl;
    431. $arrMsg[ 'msg' ] = ' Curl ERROR: curl error code is ' . $error;
    432. return $arrMsg;
    433. }
    434. if( 200 != $curl_info[ 'http_code' ] )
    435. {
    436. //异常
    437. $arrMsg[ 'code' ] = 0;
    438. $arrMsg[ 'http_code' ] = $curl_info[ 'http_code' ];
    439. $arrMsg[ 'request' ] = $remoteUrl;
    440. $arrMsg[ 'data' ] = $result;
    441. $arrMsg[ 'msg' ] = ' CURL ERROR: http response error ';
    442. return $arrMsg;
    443. }
    444. $arrMsg[ 'code' ] = $curl_info[ 'http_code' ];
    445. $arrMsg[ 'request' ] = $remoteUrl;
    446. $arrMsg[ 'data' ] = $result;
    447. $arrMsg[ 'msg' ] = 'REMOTE: http code ' . $curl_info[ 'http_code' ];
    448. return $arrMsg;
    449. }
    450. /**
    451. * 获取链接详细信息
    452. * @author Administrator
    453. * @datetime 2019年5月7日 下午5:39:16
    454. * @comment
    455. *
    456. * @param unknown $url
    457. * @return array|string[]|unknown[]|mixed[]
    458. */
    459. public function curlGetHeader( $url )
    460. {
    461. $arrRet = array();
    462. if( !$url || strlen( $url ) <= 0 )
    463. {
    464. return $arrRet;
    465. }
    466. if( !starts_with( $url , [ 'http', 'https', 'Http', 'Https', 'HTTP', 'HTTPS' ] ) )
    467. {
    468. return $arrRet;
    469. }
    470. try{
    471. $header = get_headers( $url, TRUE );
    472. if ( strpos( $header[0], 302 ) || strpos( $header[0], 301 ) )
    473. {
    474. if( isset( $header[ 'Location' ] ) && is_array( $header[ 'Location' ] ) && !empty( $header[ 'Location' ] ) )
    475. {
    476. $url = $header[ 'Location' ][count( $header['Location'] )-1 ];
    477. }
    478. else if( isset( $header[ 'Location' ] ) )
    479. {
    480. $url = $header[ 'Location' ];
    481. }
    482. }
    483. $urlInfo = parse_url( $url );
    484. if( isset( $urlInfo ) && !empty( $urlInfo ) )
    485. {
    486. $scheme = strtolower( $urlInfo[ 'scheme' ] );
    487. $arrRet[ 'scheme' ] = $scheme;
    488. $arrRet[ 'host' ] = strtolower( $urlInfo[ 'host' ] );
    489. $arrRet[ 'url' ] = $scheme . '://' . $urlInfo[ 'host' ];
    490. }
    491. }catch( \Exception $e ){
    492. if( preg_match( "/ssl3_get_server_certificate/i" , $e->getMessage() ) )
    493. {
    494. return $this->curlGetHeader( str_replace( 'https://' , 'http://', $url ) );
    495. }
    496. }
    497. return $arrRet;
    498. }
    499. }

  • 相关阅读:
    雨云游戏云面板服使用教程&我的世界Forge服务端开服教程(翼龙面板)
    尚硅谷大数据项目《在线教育之离线数仓》笔记007
    32 位计算机时间戳溢出的思考 —— 整数的二进制表示
    Java SE 9 多版本兼容 JAR 包示例
    Android 小组件 AppWidgetProvider
    【Unity】VS Code 没有自动补全 MonoBehaviour 的方法
    2022.11.24
    4.1 配置Mysql与注册登录模块
    组件设计思想
    细说tcpdump的妙用
  • 原文地址:https://blog.csdn.net/Q718330882/article/details/134250529