PHP入門之局域網多站點訪問、帶參數請求、Json數據、Xml數據

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)

  1. http://192.168.2.107/test_space/TestJson170625/xml/SampleXml.php?format=呵呵噠
2,服務器端處理方式
  1. $message = isset($_GET['format']) ? $_GET['format'] : "未上傳數據"; // 客戶端上傳的數據

3,Json數據

1,客戶端請求鏈接(電腦ip:192.168.2.107)

  1. http://192.168.2.107/test_space/TestJson170625/json/SampleJson.php

2,請求返回數據
  1. {
  2. "code": 0,
  3. "message": "成功",
  4. "data": {
  5. "id": 1,
  6. "name": "yline"
  7. }
  8. }

3,PHP處理代碼
// BaseCodeJson.php
  1. <?php
  2. class Response
  3. {
  4. /**
  5. * 按Json方式輸出通信數據
  6. *
  7. * @param number $code
  8. * 狀態嗎
  9. * @param string $message
  10. * 提示信息
  11. * @param array $data
  12. * 數據
  13. */
  14. public static function json($code = 0, $message = '', $data = array())
  15. {
  16. if (! is_numeric($code))
  17. {
  18. return '';
  19. }
  20. // 返回數據
  21. $result = array(
  22. 'code' => $code,
  23. 'message' => $message,
  24. 'data' => $data
  25. );
  26. echo json_encode($result);
  27. exit();
  28. }
  29. }
// SampleJson.php
  1. <?php
  2. require_once 'BaseCodeJson.php';
  3. $arr = array(
  4. 'id' => 1,
  5. 'name' => 'yline'
  6. );
  7. Response::json(0, "成功", $arr);

4,Xml數據

1,客戶端請求鏈接(電腦ip:192.168.2.107)

  1. http://192.168.2.107/test_space/TestJson170625/xml/SampleXml.php

2,返回數據
  1. <root>
  2. <code>0</code>
  3. <message>未上傳數據</message>
  4. <data>
  5. <id>1</id>
  6. <name>yline</name>
  7. </data>
  8. </root>

3,注意事項
1)返回Xml時,Php文件,格式
正確:<?php
錯誤:<?php   ?>
如果,選擇錯誤格式,返回xml開頭會多幾行,然後解析不出來了.
2)返回xml,需要設置:
header("Content-Type:text/xml"); // 讓網頁直接展示xml格式,而不僅僅是內容

4,PHP代碼
// BaseCodeXml.php
  1. <?php
  2. class Response
  3. {
  4. public static function xml()
  5. {
  6. $xml = "<?xml version='1.0' encoding='UTF-8'?>\n";
  7. $xml .= "<root>\n";
  8. $xml .= "<code>200</code>\n";
  9. $xml .= "<message>數據返回成功</message>\n";
  10. $xml .= "<data>\n";
  11. $xml .= "<id>200</id>\n";
  12. $xml .= "<name>yline</name>\n";
  13. $xml .= "</data>\n";
  14. $xml .= "</root>";
  15. return $xml;
  16. }
  17. /**
  18. *
  19. * @param number $code
  20. * @param string $message
  21. * @param array $data
  22. * array不能爲數字
  23. * @return string
  24. */
  25. public static function xmlEncode($code = 0, $message = '', $data = array())
  26. {
  27. if (! is_numeric($code))
  28. {
  29. return '';
  30. }
  31. // 返回數據
  32. $result = array(
  33. 'code' => $code,
  34. 'message' => $message,
  35. 'data' => $data
  36. );
  37. $xml = "<?xml version='1.0' encoding='UTF-8'?>\n";
  38. $xml .= "<root>\n";
  39. $xml .= self::xmlToEncode($result);
  40. $xml .= "</root>";
  41. return $xml;
  42. }
  43. /**
  44. *
  45. * @param unknown $data
  46. * 數組不能爲數字,否則出錯
  47. * @return string
  48. */
  49. public static function xmlToEncode($data)
  50. {
  51. $tempXml = "";
  52. foreach ($data as $key => $value)
  53. {
  54. $tempXml .= "<{$key}>";
  55. if (is_array($value))
  56. {
  57. $tempXml .= self::xmlToEncode($value);
  58. } else
  59. {
  60. $tempXml .= $value;
  61. }
  62. $tempXml .= "</{$key}>\n";
  63. }
  64. return $tempXml;
  65. }
  66. }
// SampleXml.php
  1. <?php
  2. /**
  3. * 1,組裝字符串
  4. * 2,DomDocument
  5. * 3,XMLWriter
  6. * 4,SimpleXml
  7. */
  8. require_once 'BaseCodeXml.php';
  9. header("Content-Type:text/xml"); // 讓網頁直接展示xml格式,而不僅僅是內容
  10. /* echo Response::xml(); // 字符串拼接 */
  11. $arr = array(
  12. 'id' => 1,
  13. 'name' => 'yline'
  14. );
  15. $message = isset($_GET['format']) ? $_GET['format'] : "未上傳數據"; // 客戶端上傳的數據
  16. echo Response::xmlEncode(0, $message, $arr); // 通過封裝

有技術上的問題,或者想法,歡迎來交流
QQ聯繫:[email protected]  // 備註 CSDN
github:https://github.com/yline


















發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章