2015-10-04 56 views
0

如何引用在另一個通過ajax調用的php文件中的一個php文件中創建的類對象?如何通過ajax傳遞Tumblr類對象

我有一個使用的tumblr的API,使帖子到博客項目

有2個文件:tumblr.phpajaxTumblr.php

tumblr.php,創建的tumblr對象:

include_once ("Tumblr/API/Client.php"); 
include_once ("Tumblr/API/RequestException.php"); 
include_once ("Tumblr/API/RequestHandler.php"); 

$consumerKey = "xxxxxx"; 
$consumerSecret = "xxxxxx"; 

$client = new Tumblr\API\Client($consumerKey, $consumerSecret); 
$requestHandler = $client->getRequestHandler(); 
$requestHandler->setBaseUrl('https://www.tumblr.com/'); 

然後它通過oauth序列,最終以$client完全填充訪問令牌& token_secret。我省略了代碼,因爲它可能不相關。

現在用戶進行驗證,我可以做的東西像顯示用戶的信息

$info = $client->getUserInfo();

甚至使後與一些預設數據。

$postData = array('title' => $title, 'body' => $body, 'format' => $format); 
$client->createPost($userid, $postData); 

到目前爲止,這麼好。

現在,我從用戶(標題&博客文本)文本域通過收集數據併發送一個Ajax請求ajaxTumblr.php這個信息

 $.ajax({ 
      url: 'http://domain.com/ajaxTumblr.php', 
      type:'POST', 
      async: "false", 
      data: {'title': title, 
       'body': body, 
       'userid': userid, 
       'state': "draft", 
       'format': "html" }, 
      success: function (res) { 
       console.log (res); 
      } 
     }); 

下面是我在哪裏卡住了。

我們如何在ajax php文件中傳遞,引用或重新創建$client?我無法生成新的Tumblr對象,因爲它需要用戶授權。

我想有我的代碼執行類似:

$title = $_REQUEST['title']; 
$body = $_REQUEST['body']; 
$format = $_REQUEST['format']; 
$userid = $_REQUEST['userid']; 

$postData = array('title' => $title, 'body' => $body, 'format' => $format); 
$client->createPost($userid, $postData); 

感謝。

更新

Tumblr.php,我救$client

$_SESSION['client'] = serialize($client);

然後在ajaxTumblr.php創建了一個新對象,並複製原始對象到新的一個。這可以嗎?

include_once ("Tumblr/API/Client.php"); 
include_once ("Tumblr/API/RequestException.php"); 
include_once ("Tumblr/API/RequestHandler.php"); 

// OAuth Consumer Key: 
$consumerKey = "xxxxxxx"; 
$consumerSecret = "xxxxxxxx"; 
$ajaxClient = new Tumblr\API\Client($consumerKey, $consumerSecret); 

$client = unserialize($_SESSION['client']); 
$ajaxClient = $client; 

當我重新測試,數以千計的錯誤是從類模塊抱怨的參數是不正確的一個拋出。我可能會走在正確的道路上,但需要確定。

[04-Oct-2015 14:46:27 America/Chicago] PHP Warning: curl_multi_add_handle() expects parameter 1 to be resource, integer given in /home3/me/vendor/guzzle/guzzle/src/Guzzle/Http/Curl/CurlMulti.php on line 181 
[04-Oct-2015 14:46:27 America/Chicago] PHP Warning: curl_multi_exec() expects parameter 1 to be resource, integer given in /home3/me/vendor/guzzle/guzzle/src/Guzzle/Http/Curl/CurlMulti.php on line 238 
[04-Oct-2015 14:46:27 America/Chicago] PHP Warning: curl_multi_info_read() expects parameter 1 to be resource, integer given in /home3/me/vendor/guzzle/guzzle/src/Guzzle/Http/Curl/CurlMulti.php on line 254 
[04-Oct-2015 14:46:27 America/Chicago] PHP Warning: curl_multi_exec() expects parameter 1 to be resource, integer given in /home3/me/vendor/guzzle/guzzle/src/Guzzle/Http/Curl/CurlMulti.php on line 238 
[04-Oct-2015 14:46:27 America/Chicago] PHP Warning: curl_multi_info_read() expects parameter 1 to be resource, integer given in /home3/me/vendor/guzzle/guzzle/src/Guzzle/Http/Curl/CurlMulti.php on line 254 
[04-Oct-2015 14:46:27 America/Chicago] PHP Warning: curl_multi_exec() expects parameter 1 to be resource, integer given in /home3/me/vendor/guzzle/guzzle/src/Guzzle/Http/Curl/CurlMulti.php on line 238 

回答

0

在Tumblr應用程序設置中,它要求您輸入「默認回調URL」。這設置爲"tumblr.php",因此該應用可以執行身份驗證活動。

ajaxTumblr.php嘗試執行API調用時,似乎出現此問題。回調不等於調用模塊。

此外,$client$ajaxClient對象之間存在細微的差異。其中一個元素中存在空資源,導致上面顯示的錯誤。

這些問題拖我,我決定放棄ajaxTumblr.php並在Ajax調用點的URL tumblr.php(即本身),並在頂部增加了一些邏輯來檢測,如果這是一個$_POST請求。

因此,$client對象是相同的,我設法讓應用程序發佈到我的Tumblr博客。

這裏是我在tumblr.php

if (isset($_POST['blogTitle'])){ 
    $token = $_SESSION['token'] ; 
    $secret = $_SESSION['secret'] ; 
    $client = new Tumblr\API\Client($consumerKey, $consumerSecret, $token, $secret); 

    $blogName = $_POST['blogName']; 
    $blogTitle = $_POST['blogTitle']; 
    $blogBody = $_POST['blogBody']; 
    <snip> 
    $postData = array('title' => $blogTitle, 'body' => $blogBody, 'format' => $format, 'state' => $state); 
    $res = $client->createPost($blogName, $postData); 

    exit; 
} 
+0

添加您可以標記自己的答案爲「接受」,如果它解決了這個代碼段。 –