php-snapchat icon indicating copy to clipboard operation
php-snapchat copied to clipboard

Upload function returns false...

Open KyleBoyer opened this issue 9 years ago • 6 comments

I have tried using multiple different accounts, different PHP hosts(to eliminate issues with my IP), and even very basic code, however it never is able to upload an image...

KyleBoyer avatar Dec 30 '14 18:12 KyleBoyer

No PHP code, I can't exactly help you... What type of image are you trying to upload (JPEG / GIF /PNG), file size, dimensions ? Are you authenticated correctly ?

liamjack avatar Dec 30 '14 19:12 liamjack

No PHP code, I can't exactly help you... What type of image are you trying to upload (JPEG / GIF /PNG), file size, dimensions ? Are you authenticated correctly ?

I'll post my basic code below: I'm using the default snapchat.php, snapchat_agent.php, and snapchat_cache.php inside a dependencies directory, so I won't post those... Upload.php

<?PHP
include_once "auth.php";
include_once 'dependencies/snapchat.php';
if (isset($_POST['submit'])){
    $imagedata = file_get_contents($_FILES['image']['tmp_name']);
    echo '<br><img alt="Embedded Image" src="data:image/png;base64,' . base64_encode($imagedata) . '" />';
    //$imagedata = gzencode($imagedata);
    $sendid = $snapchat->upload(0, $imagedata);
    echo "<br>Send ID: " . ($sendid) . ".";
    $sendstory = $snapchat->setStory($sendid, Snapchat::MEDIA_IMAGE, 10);
    echo "<br>Send Story Result: " . ($sendid != "" ? "true" : "false");
}else{
    echo "<form method=\"post\" enctype=\"multipart/form-data\">
    Select image to upload:
    <input type=\"file\" name=\"image\" id=\"image\">
    <input type=\"submit\" value=\"Upload Image\" name=\"submit\">
</form>";
}
?>

Auth.php

<?PHP
include_once "dependencies/snapchat.php";
$snapchat = new Snapchat('MY_USERNAME',null,'VALID_ACCESS_TOKEN');
if (!$snapchat->username) {
$snapchat = new Snapchat('MY_USERNAME','MY_PASSWORD');
}
?>

And I just tried uploading this basic image: http://www.library.nuigalway.ie/media/training/afternoon/jpeg.jpg

KyleBoyer avatar Dec 30 '14 19:12 KyleBoyer

You need to do more error checking, what happens if your $snapchat->upload doesn't work ? Are you sure you are using the latest snapchat_agent.php file sending 8.1.1 as the Android version ?

liamjack avatar Dec 30 '14 20:12 liamjack

I am using the lastest version! And that is my problem, the upload function always returns false, rather than an ID.

My $snapchat->upload function looks like this:

    public function upload($type, $data) {
        // Make sure we're logged in and have a valid access token.
        if (!$this->auth_token || !$this->username) {
            return FALSE;
            throw new Exception('Not logged in...');
        }

        // To make cURL happy, we write the data to a file first.
        $temp = tempnam(sys_get_temp_dir(), 'Snap');
        file_put_contents($temp, parent::encryptECB($data));

        if (version_compare(PHP_VERSION, '5.5.0', '>=')) {
            $cfile = curl_file_create($temp, ($type == self::MEDIA_IMAGE ? 'image/jpeg' : 'video/quicktime'), 'snap');
        }

        $media_id = strtoupper($this->username) . '~' . time();
        $timestamp = parent::timestamp();
        $result = parent::post(
            '/upload',
            array(
                'media_id' => $media_id,
                'type' => $type,
                'data' => (version_compare(PHP_VERSION, '5.5.0', '>=') ? $cfile : '@' . $temp . ';filename=data'),
                'timestamp' => $timestamp,
                'username' => $this->username,
            ),
            array(
                $this->auth_token,
                $timestamp,
            ),
            TRUE
        );

        unlink($temp);

        return is_null($result) ? $media_id : FALSE;
    }

And the problem is that the function doesn't return the media_id, which should mean that the $result is not null. I alter the last line so I can see what the $result is(because it's not null) and it comes back as a boolean that is false. Looks like the post function returning false to the upload function inside this if statement:

if ($result === FALSE || curl_getinfo($ch, CURLINFO_HTTP_CODE) != 200) {
            curl_close($ch);
            return FALSE;
        }

Specifically from this part of the if statement: curl_getinfo($ch, CURLINFO_HTTP_CODE) != 200 I add this line: return curl_getinfo($ch, CURLINFO_HTTP_CODE); and it returns a header code: 401 Unauthorized?

You need to do more error checking, what happens if your $snapchat->upload doesn't work ? Are you sure you are using the latest snapchat_agent.php file sending 8.1.1 as the Android version ?

KyleBoyer avatar Dec 31 '14 02:12 KyleBoyer

The current upload function is broken, I just tested my own code that had previously worked, we need to upgrade to the latest endpoint to get it working.

liamjack avatar Jan 02 '15 10:01 liamjack

I've got the upload and send function to work on the new endpoint, here is my testing code : http://files.lab.cuonic.com/NWI2MzJ

liamjack avatar Jan 03 '15 02:01 liamjack