Skip to content
This repository was archived by the owner on Jan 23, 2019. It is now read-only.

Commit b43a96a

Browse files
committed
some update, add new files
1 parent 6f0f105 commit b43a96a

File tree

4 files changed

+423
-2
lines changed

4 files changed

+423
-2
lines changed

src/files/File.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,17 @@
1919
*/
2020
class File extends AbstractFileSystem
2121
{
22-
public static function getName($file, $ext=null)
22+
/**
23+
* 获得文件名称
24+
* @param string $file
25+
* @param bool $clearExt 是否去掉文件名中的后缀,仅保留名字
26+
* @return string
27+
*/
28+
public static function getName($file, $clearExt=false)
2329
{
24-
return basename( trim($file), $ext);
30+
$filename = basename( trim($file) );
31+
32+
return $clearExt ? strstr($filename,'.', true) : $filename;
2533
}
2634

2735
/**
@@ -36,6 +44,11 @@ public static function getSuffix($filename, $clearPoint=false)
3644
return (bool)$clearPoint ? trim($suffix,'.') : $suffix;
3745
}
3846

47+
/**
48+
* 获得文件扩展名、后缀名,没有带点 jpg
49+
* @param $path
50+
* @return string
51+
*/
3952
public static function getExtension($path)
4053
{
4154
return pathinfo($path,PATHINFO_EXTENSION);

src/helpers/CurlHelper.php

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
<?php
2+
/**
3+
* Created by sublime 3.
4+
* Auth: Inhere
5+
* Date: 16-07-25
6+
* Time: 10:35
7+
*/
8+
9+
namespace inhere\tools\helpers;
10+
11+
use inhere\tools\files\File;
12+
13+
/**
14+
*
15+
*/
16+
class CurlHelper
17+
{
18+
private static $retriableErrorCodes = [
19+
CURLE_COULDNT_RESOLVE_HOST,
20+
CURLE_COULDNT_CONNECT,
21+
CURLE_HTTP_NOT_FOUND,
22+
CURLE_READ_ERROR,
23+
CURLE_OPERATION_TIMEOUTED,
24+
CURLE_HTTP_POST_ERROR,
25+
CURLE_SSL_CONNECT_ERROR,
26+
];
27+
28+
/**
29+
* @param string $imgUrl image url e.g. http://static.oschina.net/uploads/user/277/554046_50.jpg
30+
* @param string $savePath 图片保存路径
31+
* @param string $rename 图片重命名(只写名称,不用后缀) 为空则使用原名称
32+
*/
33+
public static function fetchImg($imgUrl, $savePath, $rename = '')
34+
{
35+
$ch = curl_init();
36+
37+
curl_setopt($ch, CURLOPT_URL, UrlHelper::encode2($imgUrl));
38+
// curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
39+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
40+
// 伪造网页来源地址,伪造来自百度的表单提交
41+
curl_setopt($ch, CURLOPT_REFERER, "http://www.baidu.com");
42+
43+
$imgData = self::execute($ch);
44+
45+
// e.g. http://static.oschina.net/uploads/user/277/554046_50.jpg?t=34512323
46+
if ( strpos($imgUrl, '?')) {
47+
list($real,) = explode('?', $imgUrl, 2);
48+
} else {
49+
$real = $imgUrl;
50+
}
51+
52+
$suffix = File::getSuffix($real);
53+
$name = $rename ? : File::getName($real, 1);
54+
$imgFile = $savePath . '/' . $name .$suffix;
55+
56+
file_put_contents($imgFile, $imgData);
57+
58+
return $imgFile;
59+
}
60+
61+
/**
62+
* send GET request
63+
* @param string $url url
64+
* @param array $params url params
65+
* @param array $headers HEADER info
66+
* @return string
67+
*/
68+
public static function get($url, array $params = [], array $headers = [])
69+
{
70+
if ($params) {
71+
$url .= (strpos($url, '?') ? '&' : '?') . http_build_query($params);
72+
}
73+
74+
// $headers = [ 'Content-Type: application/json' ];
75+
76+
$ch = curl_init();
77+
78+
curl_setopt($ch, CURLOPT_URL, $url);
79+
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
80+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
81+
//伪造网页来源地址,伪造来自百度的表单提交
82+
// curl_setopt($ch, CURLOPT_REFERER, "http://www.baidu.com");
83+
84+
return self::execute($ch);
85+
}
86+
87+
/**
88+
* send POST request
89+
*
90+
* @param string $url url
91+
* @param array $params url params
92+
* @param array $headers HEADER info
93+
* @return string
94+
*/
95+
public static function post($url, array $data = [], array $headers = [])
96+
{
97+
// $headers = [ 'Content-Type: application/json' ];
98+
99+
$ch = curl_init();
100+
101+
curl_setopt($ch, CURLOPT_URL, $url);
102+
curl_setopt($ch, CURLOPT_POST, true);
103+
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); // 发送数据
104+
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
105+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
106+
107+
// 执行HTTP请求
108+
return self::execute($ch);
109+
}
110+
111+
/**
112+
* Executes a CURL request with optional retries and exception on failure
113+
*
114+
* @param resource $ch curl handler
115+
* @param int $retries 重试
116+
* @throws \RuntimeException
117+
* @return string
118+
*/
119+
public static function execute($ch, $retries = 3, $closeAfterDone = true)
120+
{
121+
while ($retries--) {
122+
if ( ($ret = curl_exec($ch)) === false) {
123+
$curlErrno = curl_errno($ch);
124+
125+
if (false === in_array($curlErrno, self::$retriableErrorCodes, true) || !$retries) {
126+
$curlError = curl_error($ch);
127+
128+
if ($closeAfterDone) {
129+
curl_close($ch);
130+
}
131+
132+
throw new \RuntimeException(sprintf('Curl error (code %s): %s', $curlErrno, $curlError));
133+
}
134+
135+
continue;
136+
}
137+
138+
if ($closeAfterDone) {
139+
curl_close($ch);
140+
}
141+
break;
142+
}
143+
144+
return $ret;
145+
}
146+
}

src/utils/BDApi.php

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
<?php
2+
/**
3+
* Created by sublime 3.
4+
* Auth: Inhere
5+
* Date: 16-7-28
6+
* Time: 10:35
7+
* BDApi.php baidu public api
8+
*/
9+
10+
namespace inhere\tools\utils;
11+
12+
use inhere\tools\helpers\CurlHelper;
13+
14+
/**
15+
* Baidu 提供的免费api,查询一些公共信息(ip, 天气...)
16+
*/
17+
class BDApi
18+
{
19+
const OPEN_API = 'http://apis.baidu.com/apistore';
20+
21+
/**
22+
* ip地址信息查询
23+
* @param stirng $ip ip address
24+
* @param stirng $apikey 百度apikey
25+
* @return array
26+
*/
27+
public static function ipInfo($ip, $apikey)
28+
{
29+
$url = self::OPEN_API. '/iplookupservice/iplookup';
30+
31+
return self::send($url, ['ip' => $ip], ["apikey: $apikey"]);
32+
}
33+
34+
/**
35+
* 手机号码归属地的查询,获取号码在的省份以及对应的运营商
36+
*
37+
* API JSON返回示例 :
38+
* {
39+
* errNum: 0,
40+
* errMsg: "success",
41+
* retData: {
42+
* telString: "15846530170", //手机号码
43+
* province: "黑龙江", // 省份
44+
* carrier: "黑龙江移动" // 运营商
45+
* }
46+
* }
47+
* @param stirng $mobilephone mobile phone number
48+
* @param stirng $apikey 百度apikey
49+
* @return array
50+
*/
51+
public static function mobilePhoneInfo($mobilephone, $apikey)
52+
{
53+
$url = self::OPEN_API. '/mobilephoneservice/mobilephone';
54+
55+
return self::send($url, ['tel' => $mobilephone], ["apikey: $apikey"]);
56+
}
57+
58+
/**
59+
* 查询手机号的归属地信息
60+
*
61+
* api JSON返回示例 :
62+
* {
63+
* "errNum": 0,
64+
* "retMsg": "success",
65+
* "retData": {
66+
* "phone": "15210011578", // 手机号码
67+
* "prefix": "1521001", // 手机号码前7位
68+
* "supplier": "移动 ", // 移动
69+
* "province": "北京 ", // 省份
70+
* "city": "北京 ", // 城市
71+
* "suit": "152卡" // 152卡
72+
* }
73+
* }
74+
* @param stirng $mobilenumber mobile phone number
75+
* @param stirng $apikey 百度apikey
76+
* @return array
77+
*/
78+
public static function mobileNumberInfo($mobilenumber, $apikey)
79+
{
80+
$url = self::OPEN_API. '/mobilenumber/mobilenumber';
81+
82+
return self::send($url, ['tel' => $mobilephone], ["apikey: $apikey"]);
83+
}
84+
85+
/**
86+
* 彩票种类查询
87+
* @param stirng $lotteryType 彩票类型,
88+
* 1 表示全国彩 e.g. 双色球,七星彩,超级大乐透,
89+
* 2 表示高频彩票,
90+
* 3 表示低频彩票,
91+
* 4 标识境外高频彩票,
92+
* 5 标识境外低频彩票
93+
* @param stirng $apikey 百度apikey
94+
* @return array
95+
*/
96+
public static function lotteryList($lotteryType, $apikey)
97+
{
98+
$url = self::OPEN_API. '/lottery/lotterylist';
99+
100+
return self::send($url, ['lotterytype' => $lotteryType], ["apikey: $apikey"]);
101+
}
102+
103+
/**
104+
* 提供彩票最新开奖、历史开奖结果查询
105+
*
106+
* @param stirng $lotteryCode 彩票编号,通过彩票种类查询接口可获得
107+
* @param stirng $apikey 百度apikey
108+
* @param int $recordcnt 记录条数,范围为1~20
109+
* @return array
110+
*/
111+
public static function lotteryQuery($lotteryCode, $apikey, $recordcnt = 2)
112+
{
113+
$url = self::OPEN_API. 'lottery/lotteryquery';
114+
115+
return self::send($url, [
116+
'lotterycode' => $lotteryCode,
117+
'recordcnt' => $recordcnt,
118+
], ["apikey: $apikey"]);
119+
}
120+
121+
protected static function send($url, array $params = [], array $headers = [])
122+
{
123+
$res = CurlHelper::get($url, $params , $headers);
124+
125+
return json_decode($res, true);
126+
}
127+
}

0 commit comments

Comments
 (0)