|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of PHP CS Fixer. |
| 5 | + |
| 6 | + * This source file is subject to the MIT license that is bundled |
| 7 | + * with this source code in the file LICENSE. |
| 8 | + */ |
| 9 | + |
| 10 | +namespace Kcloze\Bot\Api; |
| 11 | + |
| 12 | +use GuzzleHttp\Client; |
| 13 | + |
| 14 | +class So |
| 15 | +{ |
| 16 | + const SO_IMG_URL = 'http://m.image.so.com/i'; |
| 17 | + private $client; |
| 18 | + |
| 19 | + public function __construct() |
| 20 | + { |
| 21 | + $this->client = new Client(); |
| 22 | + } |
| 23 | + |
| 24 | + public function search($text) |
| 25 | + { |
| 26 | + $client = new Client(); |
| 27 | + $res = $client->request('GET', self::SO_IMG_URL, |
| 28 | + ['query' => ['src' => 'srp'], |
| 29 | + ['q' => urlencode($text)], |
| 30 | + ] |
| 31 | + ); |
| 32 | + $res =$res->getBody()->getContents(); |
| 33 | + //var_dump($res); |
| 34 | + $content=strstr($res,'window.srcg'); |
| 35 | + //var_dump($content); |
| 36 | + //preg_match_all('/(http:\/\/www.baidu.com\/link\?[\w=-]+?)"\s*target="_blank"\s*>(.*?)<\/a>\s*/', $res, $m); |
| 37 | + //http:\/\/p0.so.qhmsg.com\/t01ca1901b5914fc9f0.jpg |
| 38 | + preg_match_all('http?:\\\/\\\/.+\.(jpg|gif|png)', $content, $m); |
| 39 | + //preg_match_all('/(p(.*).so.qhmsg.com(.*).jpg)/i', $content, $m); |
| 40 | + var_dump($m); |
| 41 | + //var_dump($res); |
| 42 | + exit; |
| 43 | + $content=json_decode($res, true); |
| 44 | + $url =isset($content['url']) ? ' ' . $content['url'] : ''; |
| 45 | + |
| 46 | + return $content['text'] . $url; |
| 47 | + } |
| 48 | +} |
0 commit comments