<?php
class httpRequest{
function httpRequestFn($url,$type,$params,$headers){
$curl =curl_init();
$timeout = 5;
curl_setopt($curl,CURLOPT_URL,$url);
// header 共四种
// 使用 http_build_query函数转换数组 key-value enctype 中是 array('Content-type: application/x-www-form-urlencoded')
// 如果 直接给数组 array('Content-type: multipart/form-data'));
// 需要时用 json格式的字符串 json_encode, array('Content-type: application/json')) 需要使用 php://input 里获得原始输入流,再 json_decode 成对象
// 应该需要xml array('Content-type: text/xml'))
if($headers!=""){
curl_setopt($curl,CURLOPT_HTTPHEADER,$headers);
}
curl_setopt ($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt ($curl, CURLOPT_CONNECTTIMEOUT, $timeout);
switch($type){
case "GET":
curl_setopt($curl, CURLOPT_HTTPGET, true);
break;
case "POST":
curl_setopt($curl, CURLOPT_POST,true);
curl_setopt($curl,CURLOPT_HEADER,0);
curl_setopt($curl, CURLOPT_POSTFIELDS,$params);
break;
case "PUT":
curl_setopt ($curl, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt ($curl, CURLOPT_POSTFIELDS,$params);
break;
case "DELETE":
curl_setopt ($curl, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_setopt ($curl, CURLOPT_POSTFIELDS,$params);
break;
}
$result = curl_exec($curl);
return $result;
curl_close($curl);
}
}
?>
调用:
function DTDType_select(){
include_once("httpRequest.php");
$httpRequest = new httpRequest();
// 浼犲�鍦板潃
$url = "192.168.1.115:80/libinterview";
// 浼犲�绫诲瀷post
$type = "POST";
// 浼犲�鏂瑰紡 json
$headers = array('Content-type: application/json');
$params = array();
$params["SERVICE_ID"] = array(30,10,1004);
$paramsString = json_encode($params);
$result = $httpRequest->httpRequestFn($url, $type, $paramsString, $headers);
echo $result;
};
上面是php通过http协议进行数据请求通信。
下面是用js进行数据请求通信
使用http协议:XHR
var xhr = new XMLHttpRequest();
var url="http://127.0.0.1:12001/zk";
xhr.open('GET', url);
xhr.responseType = 'text';
xhr.onload = function() {
console.log(xhr.response);
};
xhr.onerror = function() {
console.log("Oops, error");
};
xhr.send();
解析:
XMLHttpRequest 是一个设计粗糙的 API,不符合关注分离(Separation of Concerns)的原则,配置和调用方式非常混乱,而且基于事件的异步模型写起来也没有现代的 Promise,generator/yield,async/await 友好。
使用http协议:Request
Request
Request定义了HTTP请求获取资源的request格式,接受两个参数: url
、 options
,实际中往往不需要手动new一个request对象,通过其他操作会返回一个request对象:
const req = new Request('http://example.com/api/data', {
method: 'POST',
headers: {
'content-type': 'application/json',
},
body: 'name=test&age=12',
mode: 'cors',
credentials: 'include',
});
console.log('request body is readed ? : ',req.bodyUsed);
req.json().then((json) => {
console.log(json);
console.log('request body is readed ? : ',req.bodyUsed);
}).catch((err) => {
console.log(err)
});
// request body is readed ? : false
// demo:13 Object {name: "test", count: 123}
// request body is readed ? : true
相对于XHR方式,Request对象可以很方便、明确地显示是否允许跨域(mode)、是否需要携带cookie(credentials)等。
Fetch 的出现就是为了解决 XHR 的问题,拿例子说明:
var xhr = new XMLHttpRequest();
xhr.open('GET', url);
xhr.responseType = 'json';
xhr.onload = function() {
console.log(xhr.response);
};
xhr.onerror = function() {
console.log("Oops, error");
};
xhr.send();
使用fetch进行远程通信(数据请求)
var myInit={
method: 'GET'
};
fetch("http://127.0.0.1:12001/zk",myInit).then(function(response) {
return response.text();
}).then(function(data) {
console.log(data);
return data;
}).catch(function(e) {
return e;
});
解释:https://segmentfault.com/a/1190000003810652