Curl

仓库地址: Curl

命名空间: EasySwoole\Curl\Request

方法列表

初始化:

  • string url 请求地址
  1. function __construct(string $url = null)

设置请求地址:

  • string url 请求地址
  1. public function setUrl(string $url):Request

添加Cookie:

  • EasySwoole\Curl\Cookie cookie
  1. public function addCookie(Cookie $cookie):EasySwoole\Curl\Request

添加POST参数:

  • EasySwoole\Curl\Field field
  • bool isFile 是否为文件
  1. public function addPost(Field $field,$isFile = false):EasySwoole\Curl\Request

添加GET参数:

  • EasySwoole\Curl\Field field
  1. public function addGet(Field $field):EasySwoole\Curl\Request

添加用户信息:

  • array opt 用户信息
  • bool isMerge 是否合并

如:opt 可以设置头部信息

  1. public function setUserOpt(array $opt,$isMerge = true):EasySwoole\Curl\Request

执行请求:

  1. public function exec():EasySwoole\Curl\Response

获得用户信息:

  1. public function getOpt():array

自定义封装示例

为了方便自己习惯再封装一下自己喜欢的套路,以下仅为示例代码:

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: root
  5. * Date: 18-10-16
  6. * Time: 下午2:16
  7. */
  8. namespace App\Utility;
  9. use EasySwoole\Curl\Field;
  10. use EasySwoole\Curl\Request;
  11. use EasySwoole\Curl\Response;
  12. class Curl
  13. {
  14. public function __construct()
  15. {
  16. }
  17. /**
  18. * @param string $method
  19. * @param string $url
  20. * @param array|null $params
  21. * @return Response
  22. */
  23. public function request(string $method, string $url, array $params = null): Response
  24. {
  25. $request = new Request( $url );
  26. switch( $method ){
  27. case 'GET' :
  28. if( $params && isset( $params['query'] ) ){
  29. foreach( $params['query'] as $key => $value ){
  30. $request->addGet( new Field( $key, $value ) );
  31. }
  32. }
  33. break;
  34. case 'POST' :
  35. if( $params && isset( $params['form_params'] ) ){
  36. foreach( $params['form_params'] as $key => $value ){
  37. $request->addPost( new Field( $key, $value ) );
  38. }
  39. }elseif($params && isset( $params['body'] )){
  40. if(!isset($params['header']['Content-Type']) ){
  41. $params['header']['Content-Type'] = 'application/json; charset=utf-8';
  42. }
  43. $request->setUserOpt( [CURLOPT_POSTFIELDS => $params['body']] );
  44. }
  45. break;
  46. default:
  47. throw new \InvalidArgumentException( "method error" );
  48. break;
  49. }
  50. if( isset( $params['header'] ) && !empty( $params['header'] ) && is_array( $params['header'] ) ){
  51. foreach( $params['header'] as $key => $value ){
  52. $string = "{$key}:$value";
  53. $header[] = $string;
  54. }
  55. $request->setUserOpt( [CURLOPT_HTTPHEADER => $header] );
  56. }
  57. if( isset( $params['opt'] ) && !empty( $params['opt'] ) && is_array( $params['opt'] ) ){
  58. $request->setUserOpt($params['opt']);
  59. }
  60. return $request->exec();
  61. }
  62. }

发起请求:

  1. <<?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: root
  5. * Date: 18-10-12
  6. * Time: 上午11:07
  7. */
  8. namespace App\HttpController;
  9. use App\Utility\Curl;
  10. use EasySwoole\Http\AbstractInterface\REST;
  11. use EasySwoole\Spl\SplString;
  12. class User extends REST
  13. {
  14. function GETTest()
  15. {
  16. $request = new Curl();
  17. $params = [
  18. 'query' => [
  19. 'nobase64' => 1,
  20. 'musicid' => '109332150',
  21. 'inCharset' => 'utf8',
  22. 'outCharset' => 'utf-8'
  23. ],
  24. 'opt' => [
  25. CURLOPT_REFERER => 'https://y.qq.com/n/yqq/song/001xiJdl0t4NgO.html'
  26. ]
  27. ];
  28. $content = $request->request('GET','https://c.y.qq.com/lyric/fcgi-bin/fcg_query_lyric.fcg', $params);
  29. $string = new SplString($content);
  30. $content = $string->regex('/\{.*\}/');
  31. $json = json_decode($content, true);
  32. $lyric = $json['lyric'];
  33. $this->response()->write(html_entity_decode($lyric));
  34. }
  35. }