【问题标题】:How to Create a secured API using PHP in xml format?如何使用 PHP 以 xml 格式创建安全 API?
【发布时间】:2015-08-13 06:44:54
【问题描述】:

我有一个电子商务网站,其中经常发生交易。现在我们正在为此开发一个 android 应用程序。所以我被要求使用 PHP 构建一个 API。我制作的 API 是 xml 格式的。但是现在因为我将通过它向他发送登录凭据,所以我害怕有人会破解它。所以有人可以帮我解决这个问题。

这是我使用 php 创建 xml API 的方式。

<?php

include 'config.php';
include 'database.php';

$sqlCat = "select category_id,image,name from table" ;
$categories = DatabaseHandler::GetAll($sqlCat);

$xml = new DomDocument("1.0","UTF-8");

$content = $xml->createElement("content");
$content = $xml->appendChild($content);

foreach($categories as $category) {
    $item = $xml->createElement("item");

    $catName = $xml->createElement("catName",htmlspecialchars($category['name']));
    $catName = $item->appendChild($catName);

    $catImage = $xml->createElement("catImage",htmlspecialchars($category['image']));
    $catImage = $item->appendChild($catImage);

    $sql = "select image,name,model,price,quantity from table;
    $results = DatabaseHandler::GetAll($sql);

    foreach($results as $key=>$result) {
        $product = $xml->createElement("product");
        $product->setattribute('id',$key);

        $model = $xml->createElement("model",$result['model']);
        $model = $product->appendChild($model);

        $name = $xml->createElement("name",htmlspecialchars($result['name']));
        $name = $product->appendChild($name);

        $image = $xml->createElement("image",htmlspecialchars($result['image']));
        $image = $product->appendChild($image);

        $price = $xml->createElement("price",$result['price']);
        $price = $product->appendChild($price);

        $product = $item->appendChild($product);    
    }

    $item = $content->appendChild($item);
}

$xml->FormatOutput = true;
$output = $xml->saveXML();
$xml->save("categories.xml");


?>

我得到了这种形式的 xml..

<content>
<item>
<catName>Comp</catName>
<catImage/>
<product id="0">
<model>156443</model>
<name>CD</name>
<image>109.jpg</image>
<price>48</price>
</product>
<product id="1">
<model>46876</model>
<name>memory card</name>
<image>81.jpg</image>
<price>12</price>
</product>
<product id="2">
<model>865793</model>
<name>drive</name>
<image>51.png</image>
<price>2</price>
</product>
</item>
</content>

谁能告诉我生成 XML 格式 API 的方式是否正确。

【问题讨论】:

    标签: php xml api


    【解决方案1】:

    不要想得太复杂,像下面的代码一样简单

    $con = mysql_connect("localhost","root","root") 或 die("无法连接".mysql_error());

    //select your database table
    
    mysql_select_db("android_maps", $con) or die("Could not select database".mysql_error());
    
    //get your results with a mysql query
    
    $result = mysql_query("SELECT gmaps.*, urls.* FROM gmaps, urls WHERE urls.idgmaps = gmaps.idgmaps") or die(mysql_error());
    
    //run a while loop get your data.
    
    while($location = mysql_fetch_array($result)) {
    
    $output = '<?xml version="1.0" encoding="utf-8"?>';
    $output .= '<data>';
    $output .= "<lat>".$location['lat']."</lat>";
    $output .= "<long>.".$location['lon']."</long>";
    $output .= "<description>.".$location['description']."</description>";
    $output .= '</data>';
    print $output;
    
    }
    
    mysql_close($con);
    
    }else{
    
    echo "<h1>No results to show for you query</h1>";
    
    }
    

    更多信息

    http://teachingyou.net/php/php-api-development-dreaming-of-your-own-api-make-it-possible-today/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-05-28
      • 1970-01-01
      • 2011-08-26
      • 1970-01-01
      • 2011-01-01
      • 2011-02-18
      • 2013-06-10
      相关资源
      最近更新 更多