Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
chumoe committed May 8, 2023
1 parent be2edb9 commit adf9cb0
Show file tree
Hide file tree
Showing 4 changed files with 274 additions and 5 deletions.
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
"homepage": "http://querylist.cc",
"require": {
"PHP":">=7.1",
"jaeger/g-http": "^1.7",
"guzzlehttp/guzzle": "^7.0",
"ext-dom": "*",
"tightenco/collect": ">5.0"
"tightenco/collect": "^9.0"
},
"suggest":{

Expand All @@ -21,7 +21,8 @@
],
"autoload":{
"psr-4":{
"QL\\":"src"
"QL\\":"src",
"Jaeger\\": "src/Jaeger"
},
"files":["./src/phpQuery.php"]
},
Expand Down
155 changes: 155 additions & 0 deletions src/Jaeger/GHttp.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
<?php

/**
* 基于GuzzleHttp的简单版Http客户端。 Simple Http client base on GuzzleHttp
*
* @Author: Jaeger <JaegerCode@gmail.com>
*
* @Version V1.0
*/

namespace Jaeger;

use GuzzleHttp\Client;

/**
* Class GHttp
* @package Jaeger
*
* @method static string get($url,$args = null,$otherArgs = [])
* @method static mixed getJson($url, $args = null, $otherArgs = [])
* @method static string post($url,$args = null,$otherArgs = [])
* @method static string postRaw($url, $raw = null, $otherArgs = [])
* @method static string postJson($url, $args = null, $otherArgs = [])
*/
class GHttp
{
private static $client = null;

public static function getClient(array $config = [])
{
if(self::$client == null){
self::$client = new Client($config);
}
return self::$client;
}

/**
* @param $url
* @param array $args
* @param array $otherArgs
* @return string
*/
protected static function _get($url,$args = null,$otherArgs = [])
{
is_string($args) && parse_str($args,$args);
$args = array_merge([
'verify' => false,
'query' => $args,
'headers' => [
'referer' => $url,
'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'
]
],$otherArgs);
$client = self::getClient();
$response = $client->request('GET', $url,$args);
return (string)$response->getBody();
}

protected static function _getJson($url, $args = null, $otherArgs = [])
{
$data = self::get($url, $args , $otherArgs);
return json_decode($data,JSON_UNESCAPED_UNICODE);
}

/**
* @param $url
* @param array $args
* @param array $otherArgs
* @return string
*/
protected static function _post($url,$args = null,$otherArgs = [])
{
is_string($args) && parse_str($args,$args);
$args = array_merge([
'verify' => false,
'form_params' => $args,
'headers' => [
'referer' => $url,
'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'
]
],$otherArgs);
$client = self::getClient();
$response = $client->request('Post', $url,$args);
return (string)$response->getBody();
}

/**
* @param $url
* @param null $raw
* @param array $otherArgs
* @return string
*/
protected static function _postRaw($url, $raw = null, $otherArgs = [])
{
is_array($raw) && $raw = json_encode($raw);
$args = array_merge([
'verify' => false,
'body' => $raw,
'headers' => [
'referer' => $url,
'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'
]
],$otherArgs);
$client = self::getClient();
$response = $client->request('Post', $url,$args);
return (string)$response->getBody();
}

/**
* @param $url
* @param null $args
* @param array $otherArgs
* @return string
*/
protected static function _postJson($url, $args = null, $otherArgs = [])
{
is_string($args) && parse_str($args,$args);
$args = array_merge([
'verify' => false,
'json' => $args,
'headers' => [
'referer' => $url,
'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'
]
],$otherArgs);
$client = self::getClient();
$response = $client->request('Post', $url,$args);
return (string)$response->getBody();
}

/**
* @param $url
* @param $filePath
* @param null $args
* @param array $otherArgs
* @return string
*/
public static function download($url,$filePath,$args = null,$otherArgs = [])
{
$otherArgs = array_merge($otherArgs,[
'sink' => $filePath,
]);
return self::get($url,$args,$otherArgs);
}

/**
* @param $urls
* @return MultiRequest
*/
public static function multiRequest($urls)
{
$client = self::getClient();
return MultiRequest::newRequest($client)->urls($urls);
}
}
113 changes: 113 additions & 0 deletions src/Jaeger/MultiRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?php
/**
* Created by PhpStorm.
* User: Jaeger <JaegerCode@gmail.com>
* Date: 18/12/10
* Time: 下午6:04
*/

namespace Jaeger;
use Closure;
use GuzzleHttp\Client;
use GuzzleHttp\Pool;
use GuzzleHttp\Psr7\Request;

class MultiRequest
{
protected $client;
protected $headers = [];
protected $options = [];
protected $successCallback;
protected $errorCallback;
protected $urls = [];
protected $method;
protected $concurrency = 5;

public function __construct(Client $client)
{
$this->client = $client;
$this->headers = [
'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'
];
}

public static function newRequest(Client $client)
{
$request = new self($client);
return $request;
}

public function withHeaders($headers)
{
$this->headers = array_merge($this->headers,$headers);
return $this;
}

public function withOptions($options)
{
$this->options = $options;
return $this;
}

public function concurrency($concurrency)
{
$this->concurrency = $concurrency;
return $this;
}

public function success(Closure $success)
{
$this->successCallback = $success;
return $this;
}

public function error(Closure $error)
{
$this->errorCallback = $error;
return $this;
}

public function urls(array $urls)
{
$this->urls = $urls;
return $this;
}

public function get()
{
$this->method = 'GET';
$this->send();
}

public function post()
{
$this->method = 'POST';
$this->send();
}

protected function send()
{
$client = $this->client;

$requests = function ($urls) use($client){
foreach ($urls as $url) {
if (is_string($url)) {
yield new Request($this->method,$url,$this->headers);
} else {
yield $url;
}
}
};

$pool = new Pool($client, $requests($this->urls), [
'concurrency' => $this->concurrency,
'fulfilled' => $this->successCallback,
'rejected' => $this->errorCallback,
'options' => $this->options
]);

$promise = $pool->promise();
$promise->wait();
}

}
4 changes: 2 additions & 2 deletions src/Services/MultiRequestService.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
namespace QL\Services;


use Jaeger\GHttp;
use Closure;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Psr7\Response;
use Jaeger\GHttp;
use QL\QueryList;
use GuzzleHttp\Exception\RequestException;

/**
* Class MultiRequestService
Expand Down

0 comments on commit adf9cb0

Please sign in to comment.