如何通过ajax传递Tumblr类对象

时间:2015-10-04 19:00:15

标签: php ajax tumblr

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

我有一个项目使用Tumblr的API来发布博客

有两个文件: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);

到目前为止,非常好。

现在我通过textareas从用户收集数据(标题和博客文本),并使用此信息向ajaxTumblr.php发送ajax请求

        $.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

1 个答案:

答案 0 :(得分:0)

在Tumblr应用设置中,它会询问您“默认回调网址”。这设置为"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;
}