PHP code example of yxdj / network
1. Go to this page and download the library: Download yxdj/network library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
yxdj / network example snippets
//GET请求
$http->get(string $url[, array $get[, array $cookie]]);
//HEAD请求
$http->head(string $url[, array $get[, array $cookie]]);
//POST请求
$http->post(string $url[, array $get[, array $cookie[, array $file]]]);
//自定义请求
$http->request([
//必需参数
'url'=>'http://example.com/path/to/test.php',
//基础参数(可选)
'method' => 'POST',
'row' => ['Accept-Encoding'=>'gzip, deflate',...],
'get' => ['get_name'=>'get_value',...],
'post' => ['post_name'=>'post_value',...],
'cookie' => ['cookie_name'=>'cookie_value',...],
'file' => [
'file_name'=>['name'=>'filename','value'=>'filevalue']
...
],
//高级参数(可选)
'ip' => [], //访问域名所对应主机的ip,数组表示多ip,可随机发送,设置将省去DNS解析时间
'allow'=>[], //可允许的响应码,为空表示所有,返回一个不允许的响应码会抛出一个可捕获的异常
'jump'=>-1, //302,301响应码跳转几次,-1表示不跳转
'ctimeout' => 15, //连接超时(s)
'atimeout' => 15, //访问超时(s)
'request' =>'', //要发送的请求,此参数设置后基础参数中的所有设定失效
]);
//debug信息,包括请求头,响应头,及具体的解析过程
//$content表示是否输出响应体
//$direct表示是否直接输出信息,默认在web模式下格式化
$http->getDebug([$content=false[, $direct=false]])
//响应码,如果是大于或等于900的响应码将是请求类自定义的
$http->getCode()
//请求头信息
$http->getRequest()
//响应头信息
$http->getResponse()
//响应主体
$http->getContent()
//html关键字(如果能解析到)
$http->getKeyword()
//响应的编码(如果能解析到)
$http->getCharset()
//响应体中a标签的链接(如果能解析到)
$http->a()
//响应体中img标签的链接(如果能解析到)
$http->img()
//是否请求超时
$http->isTimeout()
$http = Api::getHttp()->get('http://php.net');
//(output)PHP: Hypertext Preprocessor
echo $http->getKeyword();
//(output)utf-8
echo $http->getCharset();
//server: http://test/test.php
print_r($_GET);
print_r($_POST);
print_r($_COOKIE);
print_r($_FILES);
//client
$http = Api::getHttp()->request([
'method' => 'POST',
'url' => 'http://test/test.php',
'get' => ['get1'=>'param2', 'get2'=>['a'=>'param2a','b'=>'param2b']],
'post' => ['post1'=>'param2', 'post2'=>['a'=>'param2a','b'=>'param2b']],
'cookie' => ['cookie1'=>'param2', 'cookie2'=>['a'=>'param2a','b'=>'param2b']],
'file' => [
'file1' => ['name'=>'111.txt','value'=>'123456'],
'file2[a]' => ['name'=>'aaa.xxx','value'=>'xxxxxx'],
'file2[b]' => ['name'=>'bbb.yyy','value'=>'yyyyyy'],
],
]);
echo $http->getDebug(true);
/*
(output)
(request)
POST /test.php?get1=param2&get2%5Ba%5D=param2a&get2%5Bb%5D=param2b HTTP/1.1
Host: test
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:33.0) Gecko/20100101 Firefox/33.0
Connection: Close
Content-Type: multipart/form-data; boundary=yxdj274972258
Content-Length: 976
Cookie: cookie1=param2; cookie2%5Ba%5D=param2a; cookie2%5Bb%5D=param2b
--yxdj274972258
Content-Disposition: form-data; name="post1"
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
param2
--yxdj274972258
Content-Disposition: form-data; name="post2[a]"
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
param2a
--yxdj274972258
Content-Disposition: form-data; name="post2[b]"
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
param2b
--yxdj274972258
Content-Disposition: form-data; name="file1"; filename="111.txt"
Content-Type: application/octet-stream
Content-Transfer-Encoding: binary
123456
--yxdj274972258
Content-Disposition: form-data; name="file2[a]"; filename="aaa.xxx"
Content-Type: application/octet-stream
Content-Transfer-Encoding: binary
xxxxxx
--yxdj274972258
Content-Disposition: form-data; name="file2[b]"; filename="bbb.yyy"
Content-Type: application/octet-stream
Content-Transfer-Encoding: binary
yyyyyy
--yxdj274972258--
(response)
HTTP/1.1 200 OK
Date: Thu, 05 Mar 2015 07:43:07 GMT
Server: Apache/2.4.9 (Win64) PHP/5.5.12
X-Powered-By: PHP/5.5.12
Content-Length: 1393
Connection: close
Content-Type: text/html
(content)
Array
(
[get1] => param2
[get2] => Array
(
[a] => param2a
[b] => param2b
)
)
Array
(
[post1] => param2
[post2] => Array
(
[a] => param2a
[b] => param2b
)
)
Array
(
[cookie1] => param2
[cookie2] => Array
(
[a] => param2a
[b] => param2b
)
)
Array
(
[file1] => Array
(
[name] => 111.txt
[type] => application/octet-stream
[tmp_name] => D:\WAMP\wamp\tmp\php6099.tmp
[error] => 0
[size] => 6
)
[file2] => Array
(
[name] => Array
(
[a] => aaa.xxx
[b] => bbb.yyy
)
[type] => Array
(
[a] => application/octet-stream
[b] => application/octet-stream
)
[tmp_name] => Array
(
[a] => D:\WAMP\wamp\tmp\php609A.tmp
[b] => D:\WAMP\wamp\tmp\php609B.tmp
)
[error] => Array
(
[a] => 0
[b] => 0
)
[size] => Array
(
[a] => 6
[b] => 6
)
)
)
(recode)
resetRequest: ok |0s
parseUrl: ok(http://test/test.php) |0s
parseDomain: ok(127.0.0.1) |0s
setRequest: ok |0s
connect: ok |0.01s
writeRequest: ok |0s
readResponse: code: 200 |0s
readContent: ok |0s
close: ok |0s
over(200): 2015-03-05 15:43:07->2015-03-05 15:43:07 |0.01s
*/
//server:http://test/login.php
$username = !empty($_POST['username']) && is_string($_POST['username'])?$_POST['username']:'';
$password = !empty($_POST['password']) && is_string($_POST['password'])?$_POST['password']:'';
if ($user = findUser($username, $password)) {
$result = ['status' => 'ok', 'data' => $user];
} else {
$result = ['status' => 'ng', 'error' => 'username or password error!'];
}
echo json_encode($result);
function findUser($username, $password){
$users =[
['username'=>'test','password'=>md5('test_pwd'),'info'=>'test login success!'],
['username'=>'yxdj','password'=>md5('yxdj_pwd'),'info'=>'yxdj login success!'],
];
$find = null;
foreach ($users as $user) {
if ($user['username'] == $username) {
$find = $user;
break;
}
}
if ($find && $find['password'] == $password) {
return $find;
} else {
return null;
}
}
//client
namespace yxdj\network\api;
use yxdj\network\Api;
class TestApi extends Api
{
public static function login($data)
{
$url = 'http://test/login.php';
$username = isset($data['username']) ? $data['username'] : '';
$password = isset($data['password']) ? $data['password'] : '';
$user = ['username' => $username, 'password' => md5($password)];
$content = Api::getHttp()->post($url, $user)->getContent();
$result = json_decode($content, true);
if (isset($result['status'])) {
if ($result['status'] == 'ok') {
return $result['data']['info'];
} elseif ($result['status'] == 'ng') {
return $result['error'];
}
}
//如有需要可以在此根据请求对象中的信息调试或判断处理
return 'unknow error!';
}
}
use yxdj\network\api\TestApi;
//(output)yxdj login success!
echo TestApi::login(['username'=>'yxdj', 'password'=>'yxdj_pwd']);
//(output)username or password error!
echo TestApi::login(['username'=>'yxdj', 'password'=>'xxx']);