Papa De Beau
Papa De Beau

Reputation: 3828

Facebook API posting photos/status updates very slow

I have users stored in a data base. I call them with a these functions in a loop but it takes like 5 to 15 seconds for each post. Its not the database code but only the posting functions that make it slow. This is not all the code but what seems to me to be the most important.

Now that I look at the code I am wondering if I need to call the function GetContentsUsingCurl() in a loop. Someone helped me write this code but its slow so I am trying to tighten it up.

example code called these function in a loop:

/// loop code
{
PostPhoto($fbId, $access_token);
PostText($fbId, $access_token);
}

My Question: Can I make this faster somehow?

function PostPhoto($fbId, $access_token)
    {
        global $PIC_URL;
        global $PIC_CAPTION;

        $url = 'https://graph.facebook.com/' . $fbId . '/photos';
        $attachment =  array(
                'access_token'  => $access_token,
                'url'           => $PIC_URL
        );

        $result = GetContentsUsingCurl($url, $attachment);
        $result = json_decode($result, TRUE);
        if( isset($result['error']) ) 
        {
            echo "Error Message: ".$result['error']['message']."<br/>";
            echo "Error Type: ".$result['error']['type']."<br/>";
            echo "Error Code: ".$result['error']['code']."<br/>";
        }
        else
        {
            echo "<pre>";
            echo "Photo posted successfully!<br/>";
        }
    }

    function PostText($fbId, $access_token)
    {
        global $TWEET_URL;
        global $TEXT_MESSAGE;
        global $AD;

        $url = 'https://graph.facebook.com/' . $fbId . '/feed';
        $tweet = GetContentsUsingCurl($TWEET_URL, "");
        $tweet = "\"".trim($tweet)."\"\n\n"; 
        $attachment =  array(
                'access_token'  => $access_token,
                'message'  => $tweet.$TEXT_MESSAGE.$AD
        );

        $result = GetContentsUsingCurl($url, $attachment);
        $result = json_decode($result, TRUE);
        if( isset($result['error']) ) 
        {
            echo "Error Message: ".$result['error']['message']."<br/>";
            echo "Error Type: ".$result['error']['type']."<br/>";
            echo "Error Code: ".$result['error']['code']."<br/>";
        }
        else
        {
            echo "<pre>";
            echo "Feed posted successfully!<br/>";
        }
    }


    function GetContentsUsingCurl($url, $attachment)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $attachment);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $result = curl_exec($ch);
        curl_close ($ch);

        return $result;
    }

Upvotes: 0

Views: 164

Answers (1)

Brian
Brian

Reputation: 3914

Since you're sending the same tweet for the whole list, you can definitely get some improvement from doing this:

$tweet = GetContentsUsingCurl($TWEET_URL, "");
$message = $tweet.$TEXT_MESSAGE.$AD;
/// loop code
{
  PostPhoto($fbId, $access_token);
  PostText($fbId, $access_token, $message);
}

And then change your PostText function to:

function PostText($fbId, $access_token, $message)
{
    $url = 'https://graph.facebook.com/' . $fbId . '/feed';
    $attachment =  array(
            'access_token'  => $access_token,
            'message'  => $message
    );

    $result = GetContentsUsingCurl($url, $attachment);
    $result = json_decode($result, TRUE);
    if( isset($result['error']) ) 
    {
        echo "Error Message: ".$result['error']['message']."<br/>";
        echo "Error Type: ".$result['error']['type']."<br/>";
        echo "Error Code: ".$result['error']['code']."<br/>";
    }
    else
    {
        echo "<pre>";
        echo "Feed posted successfully!<br/>";
    }
}

Upvotes: 1

Related Questions