Skip to content

Commit

Permalink
说明文档补充
Browse files Browse the repository at this point in the history
  • Loading branch information
gaoming13 committed Jun 25, 2020
1 parent 73280a8 commit 9fd1fff
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 26 deletions.
102 changes: 76 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,38 +19,88 @@ composer require gaoming13/http-curl
use Gaoming13\HttpCurl\HttpCurl;
```

### v2.2
### 示例

```php
HttpCurl::request('http://example.com/', 'get');
// 普通GET
// curl 'http://example.com/'
$res = HttpCurl::request('http://example.com/', 'get');
```

HttpCurl::request('http://example.com/?a=123', 'get', array(
'b' => 456
```php
// GET带参数
// curl 'http://example.com/?a=123&b=456'
$res = HttpCurl::request('http://example.com/?a=123', 'get', array(
'b' => '456'
));
```

HttpCurl::request('http://example.com/', 'post', array(
'user_uid' => 'root',
'user_pwd' => '123456'
```php
// POST Form-Data(Form Data)
// curl 'http://example.com/?ac=login' \
// --data-raw 'username=root&password=123456'
$res = HttpCurl::request('http://example.com/?ac=login', 'post', array(
'username' => 'root',
'password' => '123456'
));
```

HttpCurl::request('http://example.com/', 'post', array(
'file1' => '@/data/sky.jpg',
'file2' => '@/data/bird.jpg'
));
```php
// POST RAW(Request Payload)
// curl 'http://example.com/?ac=login' \
// --data-binary '{"username":"root","password":"123456"}'
$res = HttpCurl::request('http://example.com/?ac=login', 'post', '{"username":"root","password":"123456"}', [
'Content-Type:application/json'
]);
```

HttpCurl::request('http://example.com/', 'post', array(
'file1' => '@G:\wamp\www\data\sky.jpg',
'file2' => '@G:\wamp\www\data\bird.jpg'
```php
// POST 上传图片
$res = HttpCurl::request('http://example.com/', 'post', array(
'file1' => '@/data/sky.jpg',
'file2' => '@/data/bird.jpg'
), [
'Content-Type: image/jpeg'
]);

$res = HttpCurl::request('http://example.com/', 'post', array(
'file1' => '@G:\wamp\www\data\sky.jpg',
'file2' => '@G:\wamp\www\data\bird.jpg'
));

HttpCurl::request('http://example.com/', 'post', array(
'user_uid' => 'root',
'user_pwd' => '123456'
), array('Content-Type:application/json'));
```

```
list($body, $header, $status, $errno, $error) = \Gaoming13\HttpCurl\HttpCurl::request('http://www.example.com/', 'get');
```php
list($body, $header, $status, $errno, $error) =
HttpCurl::request(
// 请求地址
'http://example.com/',
// 请求方式(post/get)
'get',
// 传递的参数(get为queryString, post为Request Payload)
array(
'username' => 'root',
'password' => '123456'
),
// 请求头(cookie写在这里)
[
'Accept: text/plain, */*; q=0.01',
'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36',
'Referer: https://www.baidu.com/',
'Cookie: BD_UPN=123253; PSTM=1592901563;'
],
// 超时时间(秒)
5
);

// 响应正文(string)(获取失败返回空字符串)
var_dump($body);
// 响应头(string)(原始格式)
var_dump($header);
// 响应状态(array)(数组)
var_dump($status);
// 错误码(int)(0表示正常响应)
// 其它错误码见:https://www.php.net/manual/zh/function.curl-errno.php
var_dump($errno);
```

$body 响应正文
Expand All @@ -60,7 +110,7 @@ $body 响应正文
<html>
<head>
<title>Example Domain</title>
...
...
```

$header 响应头
Expand Down Expand Up @@ -138,15 +188,15 @@ HttpCurl::get('http://api.example.com/?a=123&b=456', 'json');

// POST请求
HttpCurl::post('http://api.example.com/?a=123', array(
'abc'=>'123',
'efg'=>'567'
'abc'=>'123',
'efg'=>'567'
));
HttpCurl::post('http://api.example.com/', '这是post原始内容', 'json');

// POST请求, 文件上传
HttpCurl::post('http://api.example.com/', array(
'file1'=>'@/data/sky.jpg',
'file2'=>'@/data/bird.jpg',
'file1'=>'@/data/sky.jpg',
'file2'=>'@/data/bird.jpg',
));
```

Expand Down
7 changes: 7 additions & 0 deletions autoload.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php
spl_autoload_register(function ($class) {
var_dump($class);
if (false !== stripos($class, 'Gaoming13\HttpCurl')) {
require_once __DIR__.'/'.str_replace('\\', DIRECTORY_SEPARATOR, substr($class, 18)).'.php';
}
});
43 changes: 43 additions & 0 deletions demo/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
/**
* 示例
*
* @author gaoming13 <[email protected]>
* @link https://github.com/gaoming13/HttpCurl
*/

require '../autoload.php';

use Gaoming13\HttpCurl\HttpCurl;

list($body, $header, $status, $errno, $error) =
HttpCurl::request(
// 请求地址
'http://example.com/',
// 请求方式(post/get)
'get',
// 传递的参数
array(
'user_uid' => 'root',
'user_pwd' => '123456'
),
// 请求头
[
'Accept: text/plain, */*; q=0.01',
'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36',
'Referer: https://www.baidu.com/',
'Cookie: BD_UPN=123253; PSTM=1592901563;',
],
// 超时时间(秒)
5
);

// 响应正文(string)(获取失败返回空字符串)
var_dump($body);
// 响应头(string)(原始格式)
var_dump($header);
// 响应状态(array)(数组)
var_dump($status);
// 错误码(int)(0表示正常响应)
// 其它错误码见:https://www.php.net/manual/zh/function.curl-errno.php
var_dump($errno);

0 comments on commit 9fd1fff

Please sign in to comment.