慕課php開発APPインタフェース(一、二)
6896 ワード
1/APPインタフェースの概要
2/パッケージ通信インタフェース方法
1 jsonは2つの形式のデータを返す
jsonはUTF 8形式のデータのみを返し、符号化を変換するとnullを返す
練習ディレクトリの下にresponseを新規作成します.phpファイル
#返される標準フォーマットは次のとおりです.
code:ステータスコード(200400等)
Message:ヒント情報
data:データを返す
#1.パッケージはjsonデータを返す
新しいテストファイルindex.php
#xmlファイルの生成
2つの形式.
1)組立文字列
2)システムタイプの使用
DomDocument
XmlWriter
SimpleXML
以下は組立文字列形式です
♪response.phpのjson関数の下に追加
#class Responseクラス外でテスト
#xmlデータのカプセル化
返されるデータ配列には、次の2つの形式があります.
1. array('index'=>'api')
2. array(1,7,89)
xmlが返すノードkeyは数値ではありません
#統合パッケージ
この節のクラス
2/パッケージ通信インタフェース方法
1 jsonは2つの形式のデータを返す
" " //
{" ":" "} //json
jsonはUTF 8形式のデータのみを返し、符号化を変換するとnullを返す
練習ディレクトリの下にresponseを新規作成します.phpファイル
<?php
$arr = array(
'id' =>1,
'name'=>'siangwa',
'title'=>" "
);
$data = "phpstorm ";
$newdata = iconv('UTF-8','GBK',$data);
echo $newdata;
echo json_encode($newData);
表示、後の出力はnull#返される標準フォーマットは次のとおりです.
code:ステータスコード(200400等)
Message:ヒント情報
data:データを返す
#1.パッケージはjsonデータを返す
<?php
class Response{
/**
* @param $code
* @param $massage
* @param $data
*/
public static function json($code,$massage,$data){
if(!is_numeric($code)){
return '';
}
$result = array(
'code' => $code,
'massage' =>$massage,
'data'=>$data,
);
echo json_encode($result);
exit;
}
}
新しいテストファイルindex.php
<?php
require_once('./response.php');
$arr = array(
'id'=>1,
'name'=>'singwa'
);
Response::json(200,'success',$arr);
#xmlファイルの生成
2つの形式.
1)組立文字列
2)システムタイプの使用
DomDocument
XmlWriter
SimpleXML
以下は組立文字列形式です
♪response.phpのjson関数の下に追加
public static function xml(){
header('Content-Type:text/xml');
$xml = "<?xml version='1.0' encoding='utf-8' ?>";
$xml .= "<root>";
$xml .= "<code>200</code>";
$xml .= "<message>success</message>";
$xml .= "<data>";
$xml .= "<id></id>";
$xml .= "<name>singwa</name>";
$xml .= "</data>";
$xml .= "</root>";
echo $xml;
}
#class Responseクラス外でテスト
Response::xml();
#xmlデータのカプセル化
返されるデータ配列には、次の2つの形式があります.
1. array('index'=>'api')
2. array(1,7,89)
xmlが返すノードkeyは数値ではありません
/**
* @param $code
* @param $massage
* @param $data
*/
public static function xmlEncode($code,$massage,$data){
if(!is_numeric($code)){
return '';
}
$result = array(
'code'=>$code,
'message'=>$massage,
'data'=>$data,
);
header('Content-Type:text/xml');
$xml = "<?xml version='1.0' encoding='utf-8' ?>";
$xml .= "<root>";
$xml .= self::xmlToEncode($result);
$xml .= "</root>";
echo $xml;
}
public static function xmlToEncode($data){
$xml = $attr = "";
foreach($data as $key=>$value){
if(is_numeric($key)){
$attr = " id='{$key}'";
$key = "item";
}
$xml .= "<{$key}{$attr}>";
$xml .= is_array($value) ? self::xmlToEncode($value) : $value;
$xml .= "</{$key}>";
}
return $xml;
}
#統合パッケージ
const JSON = 'json';
/**
* @param $code
* @param $massage
* @param $data
* @param string $type
*/
public static function show($code,$massage,$data,$type= self::JSON){
if(!is_numeric($code)){
return '';
}
$type = isset($_GET['format']) ? $_GET['format'] : self::JSON;
$result = array(
'code' => $code,
'massage' =>$massage,
'data'=>$data,
);
if($type == 'json'){
self::json($code,$massage,$data);
exit;
}elseif($type == 'array'){
var_dump($result);
}elseif($type == 'xml'){
self::xmlEncode($code,$massage,$data);
exit;
}else{
// todo
}
}
この節のクラス
class Response{
const JSON = 'json';
/**
* @param $code
* @param $massage
* @param $data
* @param string $type
*/
public static function show($code,$massage,$data,$type= self::JSON){
if(!is_numeric($code)){
return '';
}
$type = isset($_GET['format']) ? $_GET['format'] : self::JSON;
$result = array(
'code' => $code,
'massage' =>$massage,
'data'=>$data,
);
if($type == 'json'){
self::json($code,$massage,$data);
exit;
}elseif($type == 'array'){
var_dump($result);
}elseif($type == 'xml'){
self::xmlEncode($code,$massage,$data);
exit;
}else{
// todo
}
}
/**
* @param $code
* @param $massage
* @param $data
*/
public static function json($code,$massage,$data){
if(!is_numeric($code)){
return '';
}
$result = array(
'code' => $code,
'massage' =>$massage,
'data'=>$data,
);
echo json_encode($result);
exit;
}
/**
* @param $code
* @param $massage
* @param $data
*/
public static function xmlEncode($code,$massage,$data){
if(!is_numeric($code)){
return '';
}
$result = array(
'code'=>$code,
'message'=>$massage,
'data'=>$data,
);
header('Content-Type:text/xml');
$xml = "<?xml version='1.0' encoding='utf-8' ?>";
$xml .= "<root>";
$xml .= self::xmlToEncode($result);
$xml .= "</root>";
echo $xml;
}
public static function xmlToEncode($data){
$xml = $attr = "";
foreach($data as $key=>$value){
if(is_numeric($key)){
$attr = " id='{$key}'";
$key = "item";
}
$xml .= "<{$key}{$attr}>";
$xml .= is_array($value) ? self::xmlToEncode($value) : $value;
$xml .= "</{$key}>";
}
return $xml;
}
}