GC0202 /
ThinkLibrary
| 1 | <?php |
||
| 2 | |||
| 3 | namespace DtApp\ThinkLibrary\service\weishi; |
||
| 4 | |||
| 5 | use DtApp\ThinkLibrary\facade\Pregs; |
||
| 6 | use DtApp\ThinkLibrary\Service; |
||
| 7 | use GuzzleHttp\Client; |
||
| 8 | use GuzzleHttp\Cookie\CookieJar; |
||
| 9 | use GuzzleHttp\Psr7\Stream; |
||
| 10 | |||
| 11 | /** |
||
| 12 | * Class WatermarkService |
||
| 13 | * @package DtApp\ThinkLibrary\service\weishi |
||
| 14 | */ |
||
| 15 | class WatermarkService extends Service |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * @var |
||
| 19 | */ |
||
| 20 | private $url, $contents, $backtrack; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * 设置网址 |
||
| 24 | * @param $url |
||
| 25 | * @return WatermarkService |
||
| 26 | */ |
||
| 27 | public function setUrl($url): self |
||
| 28 | { |
||
| 29 | if (Pregs::isLink($url)) { |
||
| 30 | $this->url = $url; |
||
| 31 | } else { |
||
| 32 | preg_match_all('#\bhttps?://[^,\s()<>]+(?:\([\w\d]+\)|([^,[:punct:]\s]|/))#', $url, $match); |
||
| 33 | $this->url = $match[0][0]; |
||
| 34 | } |
||
| 35 | return $this; |
||
| 36 | } |
||
| 37 | |||
| 38 | /** |
||
| 39 | * 获取接口全部信息 |
||
| 40 | * @return WatermarkService |
||
| 41 | * @throws \GuzzleHttp\Exception\GuzzleException |
||
| 42 | */ |
||
| 43 | private function getApi(): self |
||
| 44 | { |
||
| 45 | $this->contents = $this->getContents($this->url); |
||
| 46 | return $this; |
||
| 47 | } |
||
| 48 | |||
| 49 | /** |
||
| 50 | * 获取全部信息 |
||
| 51 | * @return $this |
||
| 52 | * @throws \GuzzleHttp\Exception\GuzzleException |
||
| 53 | */ |
||
| 54 | public function getAll(): self |
||
| 55 | { |
||
| 56 | $this->getApi(); |
||
| 57 | $data = [ |
||
| 58 | 'video_src' => $this->contents['video_src'], |
||
| 59 | 'cover_image' => $this->contents['cover_image'], |
||
| 60 | ]; |
||
| 61 | $this->backtrack = $data; |
||
| 62 | return $this; |
||
| 63 | } |
||
| 64 | |||
| 65 | /** |
||
| 66 | * 返回Array |
||
| 67 | * @return array|mixed |
||
| 68 | */ |
||
| 69 | public function toArray() |
||
| 70 | { |
||
| 71 | if (empty($this->backtrack)) { |
||
| 72 | return []; |
||
| 73 | } |
||
| 74 | if (is_array($this->backtrack)) { |
||
| 75 | return $this->backtrack; |
||
| 76 | } |
||
| 77 | return json_decode($this->backtrack, true); |
||
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * 获取 |
||
| 82 | * @param $url |
||
| 83 | * @return array |
||
| 84 | * @throws \GuzzleHttp\Exception\GuzzleException |
||
| 85 | */ |
||
| 86 | private function getContents($url): array |
||
| 87 | { |
||
| 88 | $url = urldecode($url); |
||
| 89 | $headers = [ |
||
| 90 | 'Connection' => 'keep-alive', |
||
| 91 | 'User-Agent' => 'Mozilla/5.0 (iPhone; CPU iPhone OS 12_1_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/16D57 Version/12.0 Safari/604.1' |
||
| 92 | ]; |
||
| 93 | $client = new Client(['timeout' => 2, 'headers' => $headers, 'http_errors' => false,]); |
||
| 94 | $data = []; |
||
| 95 | if ($headers) { |
||
|
0 ignored issues
–
show
|
|||
| 96 | $data['headers'] = $headers; |
||
| 97 | } |
||
| 98 | $data['verify'] = __DIR__ . DIRECTORY_SEPARATOR . 'cacert.pem'; |
||
| 99 | $jar = new CookieJar; |
||
| 100 | $data['cookies'] = $jar; |
||
| 101 | if (!$params) { |
||
|
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
| 102 | $response = $client->request('GET', $url, $data); |
||
| 103 | } else { |
||
| 104 | $data ['form_params'] = $params; |
||
| 105 | $response = $client->request('POST', $url, $data); |
||
| 106 | } |
||
| 107 | $body = $response->getBody(); |
||
| 108 | if ($body instanceof Stream) { |
||
| 109 | $body = $body->getContents(); |
||
| 110 | } |
||
| 111 | $result = json_decode($body, true); |
||
| 112 | $file = 'weishi.txt'; |
||
| 113 | $fp = fopen($file, 'ab'); |
||
| 114 | fwrite($fp, $body); |
||
| 115 | fclose($fp); |
||
| 116 | if ($result['ret'] == 0) { |
||
| 117 | $video = $result['data']['feeds'][0]; |
||
| 118 | $data['video_src'] = $video['video_url']; |
||
| 119 | $data['cover_image'] = $video['images'][0]['url']; |
||
| 120 | return $data; |
||
| 121 | } |
||
| 122 | return []; |
||
| 123 | } |
||
| 124 | } |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.