【问题标题】:Querying Fusion-Table from PHP从 PHP 查询 Fusion-Table
【发布时间】:2012-04-10 17:15:00
【问题描述】:

我的情况如下: 我有一个 MySQL 数据库,我将其导出并插入到 Fusion Table 中。现在我需要修改php页面从Fusion Table而不是localhost db中查询数据。

我从开发人员指南中阅读了有关查询 Fusion-Tables 的信息,但它指的是 GData Java 库和 SQL API。在另一个站点上,我看到还有其他可用于查询的库,包括 PHP 所依赖的 Zend 框架,这与我的情况相关。但是,我没有偶然发现任何示例或教程,它们只是显示了一个 php 页面,说明如何查询一个简单的单元格、行或列,我可以从中修改和改进编码以将其应用于设计。

我的问题是:

  1. 我需要在托管空间安装/上传一些库吗?

  2. 是否有关于查询 Fusion Table 数据的 php 示例和教程,我可能错过了?

  3. 谁能告诉我你将如何查询例如 ID 为 5 的行的纬度信息? (页面使用路径检索:articles.php?lang=en&pg=comm_en&m=view&commID=88

下面是我在 comm_en.php 页面中的编码,它从 PHP 查询到 SQL 数据。

提前谢谢你!

德里尼

<?php
session_start();
foreach ($_REQUEST as  $key => $value){
    $$key=addslashes(htmlspecialchars(strip_tags($value)));
}
if(isset($_GET["m"]))       {$m=$_GET["m"];}            else     {$m="";}
if(isset($_GET["commID"]))      {$commID=$_GET["commID"];}    else {$commID=1;}
if(isset($_GET["lang"]))        {$lang=$_GET["lang"];}   
else {$lang="en";}

Shfaq($m,$commID);

function Shfaq($metod,$commID)
 {
switch($metod)
{
 case "view":
        {View($commID);}        
    break;
 case "default":
        {View($artID);}      
    break;

}
}  // end of Shfaq();


function View($commID)
{
     $link =mysql_connect("localhost", "db-user", "db-pass") or die ("E pamundur lidhja!");
mysql_select_db("DataBase-name") or die (mysql_error());
$queryComm = "SELECT * FROM communities WHERE id ='$commID' LIMIT 0,1";
$commRes=mysql_query($queryComm) or die(mysql_error());
$comm=mysql_fetch_array($commRes);  
$healthPerc = round(($comm["healthBooklet"]/$comm["totalChildNo"]), 3)*100 ;

echo '      <table class="gpsbox" align="right">
        <caption>GPS Location</caption>                   
        <tr><td>Latitude </td><td>'.$comm["latitude"].'</td></tr>
        <tr><td>Longitude</td><td>'.$comm["longitude"].'</td></tr> 
    </table>....<html coding continues>

【问题讨论】:

    标签: google-fusion-tables


    【解决方案1】:

    以下是如何将 newest PHP API client library 与 Fusion Tables 一起使用:

    use Google_Service_Fusiontables;
    
    $client_email = '<id>@developer.gserviceaccount.com';
    $private_key = file_get_contents('<private key>.p12');
    $scopes= array(
            'https://www.googleapis.com/auth/fusiontables',
            'https://www.googleapis.com/auth/fusiontables.readonly',
            );
    $credentials = new Google_Auth_AssertionCredentials(
        $client_email,
        $scopes,
        $private_key
    );
    
    $client = new Google_Client();
    $client->setAssertionCredentials($credentials);
    if ($client->getAuth()->isAccessTokenExpired()) {
      $client->getAuth()->refreshTokenWithAssertion();
    }
    
    $service = new Google_Service_Fusiontables($client);
    $result = $service->query->sql('SELECT <column> FROM <table id>');
    var_dump($result);
    

    【讨论】:

      【解决方案2】:
      1. 有一个PHP client library,如果需要,可以使用它(这可能是从 PHP 访问 Fusion Tables 的最简单方法)

      2. 在客户端库旁边还有PHP sample code,看看吧,应该可以帮助你理解概念。

      3. 当你使用该库时,你可以简单地编写 sql 语句,即类似

        select latitude from 123456 where id = 5;
        

      123456 指的是你的融合表的表ID。如果您的表是公开的,您甚至不必关心身份验证,但如果您想访问私有表或更新/插入数据,我建议使用OAuth for authentication

      一般的示例代码可以在Google Fusion Tables API Sample Code上找到

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-03-06
        • 2013-07-27
        • 2012-07-01
        • 1970-01-01
        • 2013-08-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多