简体   繁体   中英

cURL PHP: uploading photo to Instagram

I authorized to Instagram, got all cookies, etc.

Now it's time to make request at https://www.instagram.com/create/upload/photo/ .

In Chrome's inspector I see that browser sends these post data (where the binary photo is probably blob):

upload_id: 1539936226445
photo: (binary)
media_type: 1

And custom headers:

x-csrftoken: ACCESS_TOKEN_THAT_I_ALREADY_WROTE_INTO_THE_FILE
x-instagram-ajax: 723425780848
x-requested-with: XMLHttpRequest

But when I do the same request using cURL (passing posts and header that I wrote upper), I get 403 Forbidden, but should get 200 Ok with JSON response.

Here is how I make request:

        $this->headers['csrftoken'] = $this->getFileData('csrftoken');
        // generate upload_id for post field
        $uploadId = number_format(round(microtime(true) * 1000), 0, '', ''); 
        // generate binary from image file (maybe not correct)          
        $file_bin = fopen($file, 'rb'); 

        // all my post fields
        $params = array(
            'upload_id' => $uploadId,
            'photo' => $file_bin,
            'media_type' => '1',
        );

        $ch = curl_init('https://www.instagram.com/create/upload/photo/');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_HEADER, true);
        // custom headers
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'x-csrftoken: '.$this->headers['csrftoken'],
            'x-instagram-ajax: '.$this->headers['x-instagram-ajax'],
            'x-requested-with: '.$this->headers['x-requested-with']
        ));

        curl_setopt($ch, CURLOPT_POST, true);
        // post fields
        curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
        curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookieFile);
        curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookieFile);
        curl_setopt($ch, CURLOPT_USERAGENT, $this->userAgent);
        // and finally get 403 forbidden instead of 200 ok with json response
        $result = curl_exec($ch);
        curl_close($ch);

        return $result;

Does anybody know where the problem can hide?

Thanks so much for the help!

您应该传递cookie数据,或在代码内启动另一个会话,然后发送cookie和该会话的csrftoken。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM