1,局域网多站点访问
1,有一个yline.php文件,本地建立了两个站点,localhost和yline;
本地浏览器打开方式:
1)localhost:80/test/yline.php
2)yline:8080/yline.php
2,局域网默认访问的是80端口,所以需要配置以下几点,才能访问多个
1)监听端口
2)多站点配置,修改对应的端口
3,局域网,打开方式则为
1)ip:80/test/yline.php
2)ip:8080/yline.php
2,带参数请求
1,客户端请求链接(电脑ip:192.168.2.107)
http://192.168.2.107/test_space/TestJson170625/xml/SampleXml.php?format=呵呵哒
2,服务器端处理方式
$message = isset($_GET['format']) ? $_GET['format'] : "未上传数据"; // 客户端上传的数据
3,Json数据
1,客户端请求链接(电脑ip:192.168.2.107)
http://192.168.2.107/test_space/TestJson170625/json/SampleJson.php
2,请求返回数据
{
"code": 0,
"message": "成功",
"data": {
"id": 1,
"name": "yline"
}
}
3,PHP处理代码
// BaseCodeJson.php
<?php
class Response
{
/**
* 按Json方式输出通信数据
*
* @param number $code
* 状态吗
* @param string $message
* 提示信息
* @param array $data
* 数据
*/
public static function json($code = 0, $message = '', $data = array())
{
if (! is_numeric($code))
{
return '';
}
// 返回数据
$result = array(
'code' => $code,
'message' => $message,
'data' => $data
);
echo json_encode($result);
exit();
}
}
// SampleJson.php
<?php
require_once 'BaseCodeJson.php';
$arr = array(
'id' => 1,
'name' => 'yline'
);
Response::json(0, "成功", $arr);
4,Xml数据
1,客户端请求链接(电脑ip:192.168.2.107)
http://192.168.2.107/test_space/TestJson170625/xml/SampleXml.php
2,返回数据
<root>
<code>0</code>
<message>未上传数据</message>
<data>
<id>1</id>
<name>yline</name>
</data>
</root>
3,注意事项
1)返回Xml时,Php文件,格式
正确:<?php
错误:<?php ?>
如果,选择错误格式,返回xml开头会多几行,然后解析不出来了.
2)返回xml,需要设置:
header("Content-Type:text/xml"); // 让网页直接展示xml格式,而不仅仅是内容
4,PHP代码
// BaseCodeXml.php
<?php
class Response
{
public static function xml()
{
$xml = "<?xml version='1.0' encoding='UTF-8'?>\n";
$xml .= "<root>\n";
$xml .= "<code>200</code>\n";
$xml .= "<message>数据返回成功</message>\n";
$xml .= "<data>\n";
$xml .= "<id>200</id>\n";
$xml .= "<name>yline</name>\n";
$xml .= "</data>\n";
$xml .= "</root>";
return $xml;
}
/**
*
* @param number $code
* @param string $message
* @param array $data
* array不能为数字
* @return string
*/
public static function xmlEncode($code = 0, $message = '', $data = array())
{
if (! is_numeric($code))
{
return '';
}
// 返回数据
$result = array(
'code' => $code,
'message' => $message,
'data' => $data
);
$xml = "<?xml version='1.0' encoding='UTF-8'?>\n";
$xml .= "<root>\n";
$xml .= self::xmlToEncode($result);
$xml .= "</root>";
return $xml;
}
/**
*
* @param unknown $data
* 数组不能为数字,否则出错
* @return string
*/
public static function xmlToEncode($data)
{
$tempXml = "";
foreach ($data as $key => $value)
{
$tempXml .= "<{$key}>";
if (is_array($value))
{
$tempXml .= self::xmlToEncode($value);
} else
{
$tempXml .= $value;
}
$tempXml .= "</{$key}>\n";
}
return $tempXml;
}
}
// SampleXml.php
<?php
/**
* 1,组装字符串
* 2,DomDocument
* 3,XMLWriter
* 4,SimpleXml
*/
require_once 'BaseCodeXml.php';
header("Content-Type:text/xml"); // 让网页直接展示xml格式,而不仅仅是内容
/* echo Response::xml(); // 字符串拼接 */
$arr = array(
'id' => 1,
'name' => 'yline'
);
$message = isset($_GET['format']) ? $_GET['format'] : "未上传数据"; // 客户端上传的数据
echo Response::xmlEncode(0, $message, $arr); // 通过封装
有技术上的问题,或者想法,欢迎来交流
QQ联系:957339173@qq.com // 备注 CSDN
github:https://github.com/yline