Skip to content

Commit bd852f3

Browse files
author
lixiaopei
committed
增加so搜索图片接口
1 parent eb5b1c9 commit bd852f3

File tree

2 files changed

+57
-3
lines changed

2 files changed

+57
-3
lines changed

src/Api/So.php

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
/*
4+
* This file is part of PHP CS Fixer.
5+
* (c) kcloze <[email protected]>
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+
}

test/api.php

+9-3
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,15 @@
1010
date_default_timezone_set('Asia/Shanghai');
1111

1212
require __DIR__ . '/../vendor/autoload.php';
13-
use Kcloze\Bot\Api\Baidu;
13+
// use Kcloze\Bot\Api\Baidu;
1414

15-
$baidu =new Baidu();
16-
$rest = $baidu->search2('众泰汽车');
15+
// $baidu =new Baidu();
16+
// $rest = $baidu->search('众泰汽车');
17+
18+
// var_dump($rest);
19+
20+
use Kcloze\Bot\Api\So;
21+
$baidu =new So();
22+
$rest = $baidu->search('众泰汽车');
1723

1824
var_dump($rest);

0 commit comments

Comments
 (0)