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); // 通過封裝