如何通过ajax传递一个Tumblr类对象



如何在通过ajax调用的另一个php文件中引用在一个php文件中创建的类对象?

我有一个项目,使用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 TumblrAPIClient($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);

到目前为止,一切顺利。

现在我从用户(title &通过文本区域发送一个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);
            } 
        });

这就是我被困住的地方。

我们如何传递,引用或重建$client在ajax php文件?我不能生成一个新的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 TumblrAPIClient($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

在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 TumblrAPIClient($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;
}

最新更新