此URL错误不支持HTTP方法POST

时间:2012-01-19 23:07:42

标签: php http post

我正在尝试使用以下代码编辑和显示谷歌融合表中的数据:

<?php

include('Lib/clientlogin.php');
include('Lib/sql.php');
include('Lib/file.php');

//get token
$token = ClientLogin::getAuthToken('usernane', 'password');
$ftclient = new FTClientLogin($token);

//show all tables
echo $ftclient->query(SQLBuilder::showTables());
echo "<br />";

//describe a table
echo $ftclient->query(SQLBuilder::describeTable(2683865));
echo "<br />";

//select * from table
echo $ftclient->query(SQLBuilder::select(2683865));
echo "<br />";

//select * from table where test=1
echo $ftclient->query(SQLBuilder::select(2683865, null, "'test'=1"));
echo "<br />";

//select test from table where test = 1
echo $ftclient->query(SQLBuilder::select(2683865, array('test'), "'test'=1"));
echo "<br />";

//select rowid from table
echo $ftclient->query(SQLBuilder::select(2683865, array('rowid')));
echo "<br />";

//delete row 401
echo $ftclient->query(SQLBuilder::delete(2683865, '401'));
echo "<br />";

//drop table
echo $ftclient->query(SQLBuilder::dropTable(358731));
echo "<br />";

//update table test=1 where rowid=1
echo $ftclient->query(SQLBuilder::update(2683865, array('test'=>12), 1));
echo "<br />";

//insert into table (test, test2, 'another test') values (12, 3.3333, 'bob')
echo $ftclient->query(SQLBuilder::insert(2683865, array('test'=>12, 'test2' => 3.33333, 'another test' => 'bob')));

?>
Hide details
Change log
r8 by kbris...@google.com on Apr 7, 2011   Diff
updates to library
Go to:  
Older revisions
 r4 by kbris...@google.com on Dec 17, 2010   Diff 
 r3 by kbris...@google.com on Dec 17, 2010   Diff 
All revisions of this file
File info
Size: 1321 bytes, 50 lines
View raw file

当我运行时,我收到错误:

HTTP method POST is not supported by this URL

Error 405

之前我在这台服务器上使用了php“POST”命令而没有问题。如果有人对可能出现的问题有任何想法,我们将不胜感激。

编辑:下面是query()函数的代码。我相信这是错误的起源:

function query($query, $gsessionid = null, $recursedOnce = false) {

    $url = SCOPE;
        $query =  "sql=".urlencode($query);

    $fusiontables_curl=curl_init();
    if(preg_match("/^select|^show tables|^describe/i", $query)) { 
          $url .= "?".$query;
      if($gsessionid) { $url .= "&gsessionid=$gsessionid"; }
      curl_setopt($fusiontables_curl,CURLOPT_HTTPHEADER, array("Authorization: GoogleLogin auth=".$this->token));

    } else {
      if($gsessionid) { $url .= "?gsessionid=$gsessionid"; }

      //set header
      curl_setopt($fusiontables_curl,CURLOPT_HTTPHEADER, array( 
        "Content-length: " . strlen($query), 
        "Content-type: application/x-www-form-urlencoded", 
        "Authorization: GoogleLogin auth=".$this->token         
      )); 

      //set post = true and add query to postfield
      curl_setopt($fusiontables_curl,CURLOPT_POST, true);
      curl_setopt($fusiontables_curl,CURLOPT_POSTFIELDS,$query); 
    }

    curl_setopt($fusiontables_curl,CURLOPT_URL,$url);
    curl_setopt($fusiontables_curl,CURLOPT_CONNECTTIMEOUT,2);
    curl_setopt($fusiontables_curl,CURLOPT_RETURNTRANSFER,1);
    $result = curl_exec($fusiontables_curl);
    curl_close($fusiontables_curl);

    //If the result contains moved Temporarily, retry
    if (strpos($result, '302 Moved Temporarily') !== false) {
      preg_match("/(gsessionid=)([\w|-]+)/", $result, $matches);

      if (!$matches[2]) { return false; }

      if ($recursedOnce === false) {
        return $this->query($url, $matches[2], true);
      }
      return false;
    }

    return $result;
  }

1 个答案:

答案 0 :(得分:1)

我不确定你的服务器是什么问题。该错误表明POST不适用于该URL。我知道Google对POST命令很敏感,如果您在正常搜索时尝试从GET切换到POST,则会拒绝您的搜索。在这种情况下可能是相同的。

尝试使用GET或检查对POST命令的调用,我在您的代码中没有看到它。

编辑:

来自:http://code.google.com/apis/fusiontables/docs/developers_guide.html

  

API语句使用HTTP GET请求(用于查询)和POST请求(用于插入,更新和删除)从Web客户端应用程序发送到Google Fusion Tables服务器。

你有它,就像我建议的那样,你必须使用GET来检索数据,并且只使用POST发送命令来编辑数据。更改query()函数以使用GET,然后编写一个单独的edit()函数,该函数使用POST编辑数据。