Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upload function returns false... #68

Open
KyleBoyer opened this issue Dec 30, 2014 · 6 comments
Open

Upload function returns false... #68

KyleBoyer opened this issue Dec 30, 2014 · 6 comments

Comments

@KyleBoyer
Copy link

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

@liamjack
Copy link

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 ?

@KyleBoyer
Copy link
Author

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

@liamjack
Copy link

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
Copy link
Author

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 ?

@liamjack
Copy link

liamjack commented Jan 2, 2015

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
Copy link

liamjack commented Jan 3, 2015

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants