如何引用在另一個通過ajax調用的php文件中的一個php文件中創建的類對象?如何通過ajax傳遞Tumblr類對象
我有一個使用的tumblr的API,使帖子到博客項目
有2個文件:tumblr.php
和ajaxTumblr.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
添加您可以標記自己的答案爲「接受」,如果它解決了這個代碼段。 –