Search code examples
phpajaxtumblr

How to pass a Tumblr class object via ajax


How do you reference a class object that is created in one php file in another php file called via ajax?

I have a project that uses Tumblr's API to make posts to a blog

There are 2 files: tumblr.php and ajaxTumblr.php

In tumblr.php, a Tumblr object is created:

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/');

Then it goes through the oauth sequence, ending up with $client fully populated with the access token & token_secret. I omitted the code for this, since it's likely not relevant.

Now the user is validated and I can do stuff like display the user's info

$info = $client->getUserInfo();

Or even make a post with some preset data.

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

So far, so good.

Now I collect data from the user (title & blog text) via textareas and send an ajax request to ajaxTumblr.php with this info

        $.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);
            } 
        });

Here's where I am stuck.

How do we pass over, reference or recreate $client in the ajax php file? I can't generate a new Tumblr object, since it requires user authorization.

I'd like to have my code perform something like:

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

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

Thanks.

Update

In Tumblr.php, I saved $client:

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

And then in ajaxTumblr.php created a new object and copied the original object to the new one. Is this okay to do?

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;

When I reran the test, thousands of errors were thrown from one of the class modules complaining about parameters being incorrect. I might be on the right path, but need conformation.

[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

Solution

  • In the Tumblr app settings, it asks you for "Default callback URL". This is set to "tumblr.php", so the app can perform authentication activities.

    The problem seemed to arise when ajaxTumblr.php tries to perform API calls. The callback does not equal the calling module.

    Further, there was a slight discrepancy between the $client and $ajaxClient objects. There was a null resource in one of the elements, causing the errors shown above.

    With these issues holding me back, I decided to ditch ajaxTumblr.php and have the URL in the ajax call point to tumblr.php (i.e. itself) and added some logic at the top to detect if this is a $_POST request.

    As a result, the $client object is identical, and I managed to get the app to post to my Tumblr blog.

    Here is a code snippet that I added in 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;
    }