这一切都始于我想在instagram上花一整天的时间搜索和测试自动发布服务的事实,然后我决定查看现成的github软件包,我对这些软件包的代码大小感到惊讶(某些php框架比instagram上的这些包装小),我吐口水,决定用最少的功能编写包装器。登录
我们将通过WEB版本传递授权。为此,我们从一开始就需要获取标题,cookie。
 $curl = curl_init();
 curl_setopt_array($curl, [
        CURLOPT_URL => 'https://www.instagram.com/',
        CURLOPT_HEADER => true,
        CURLOPT_SSL_VERIFYHOST => false,
        CURLOPT_SSL_VERIFYPEER => false,
        CURLOPT_RETURNTRANSFER => true,
        CURLINFO_HEADER_OUT => true,
        CURLOPT_HTTPHEADER => ['user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) 
        AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36'],
 ]);
 $response = curl_exec($curl);
 $headers  = curl_getinfo($curl);
 
 $header_content = substr($response, 0, $headers['header_size']);
 curl_close($curl);
$cookie = [];
preg_match_all("/Set-Cookie:\s*(?<cookie>[^=]+=[^;]+)/mi", $header_content, $matches);
foreach ($matches['cookie'] as $c) {
            if ($c = str_replace(['sessionid=""', 'target=""'], '', $c)) {
                $c = explode('=', $c);
                $cookie = array_merge($cookie, [trim($c[0]) => trim($c[1])]);
            }
        }
if (isset($cookie['csrftoken']) {
}
此外,我将不提供CURL工作的示例。授权:
POST
     https:
HEADER
     Content-Type: application/x-www-form-urlencoded
    
     x-ig-app-id: 1217981644879628
     x-csrftoken: 
     cookie: 
     user-agent: 
BODY
     username=&password=&queryParams={}&optIntoOneTap=false
 
{"authenticated": true, "user": true, "userId": "****", "loginNonce": "****", "reactivated": true, "status": "ok"}
 
{"authenticated": false, "user": true, "status": "ok"}
 
用户搜索
为此,我们将需要先前收到的cookie和x-csrftoken标头。
GET 
    https:
HEADER
     x-csrftoken: 
     x-ig-app-id: 1217981644879628
     cookie: 
     user-agent: 
 {
    "users": [
                 {"position":0, 
                   "user":{
                               "pk":" "
                               "username":"  "
                               "full_name":" ,  "
                               .....    
                               }
                  },{},{}
    ],
    "places":[   ],
    "hashtags":[  ],
    "has_more": true,
    "rank_token": "0.44093530619864296",
    "clear_client_cache": false,
    "status: "ok"
}
发送消息以直接
要发送直接消息,我们需要将发送给谁的用户ID,以及如何找到我上面描述的用户。
POST
       https:
HEADER
      
      user-agent: Instagram 10.2.2 Android (18/4.3; 320dpi; 720x1280; Huawei; HWEVA; EVA-L18; qcom; en_US)
     x-csrftoken: 
     x-ig-app-id: 1217981644879628
     cookie: 
     content-type: application/x-www-form-urlencoded
BODY
     text={ }&_uuid=&_csrftoken={   csrftoken}&recipient_users="[[   ]]"&action=send_item&thread_ids=["0"]&client_context={UUID v4  }
{"status":"ok", "payload":{"item_id":" "} ...}
function uuid4()
    {
        if (function_exists('com_create_guid') === true) {
            return trim(com_create_guid(), '{}');
        }
        $data = openssl_random_pseudo_bytes(16);
        $data[6] = chr(ord($data[6]) & 0x0f | 0x40);
        $data[8] = chr(ord($data[8]) & 0x3f | 0x80);
        return vsprintf('%s%s%s%s%s%s%s%s', str_split(bin2hex($data), 4));
    }
通过WEB版本上传照片
发送图像之前,您需要将其保存为ImageJPEG质量100,否则instagram将返回错误:$photo = __DIR__ . '/source.jpg';
$file_temp = __DIR__ . '/send_images.jpg';
list($width, $height, $image_type) = getimagesize(realpath($photo));
$srcImage = ImageCreateFromJPEG($photo);
$resImage = ImageCreateTrueColor($width, $height);
ImageCopyResampled($resImage, $srcImage, 0, 0, 0, 0, $width, $height, $width, $height);
ImageJPEG($srcImage, $file_temp, 100);
ImageDestroy($srcImage);
上传照片:
$microtime = round(microtime(true) * 1000);
POST
      https:
HEADER
      content-type: image/jpg
      x-entity-name: 'fb_uploader_' . $microtime 
      offset: 0
      user-agent: Mozilla/5.0 (iPhone; CPU iPhone OS 11_4_1 like Mac OS X; ru-RU) AppleWebKit/537.36 (KHTML, like Gecko)  Version/11.4.1 Mobile/15G77 Safari/537.36 Puffin/5.2.2IP
      x-entity-length: filesize($file_temp) 
      x-instagram-rupload-params: {"media_type":1,"upload_id":"' . $microtime . '","upload_media_height":' . $height . ',"upload_media_width":' . $width . '}
     x-csrftoken: 
     x-ig-app-id: 1217981644879628
     cookie: 
BODY
     
     file_get_contents(realpath($file_temp))
{"upload_id":"   $microtime", "status":"ok" ...}
 
 
POST
       https:
HEADER
      content-type: application/x-www-form-urlencoded
      user-agent: Mozilla/5.0 (iPhone; CPU iPhone OS 11_4_1 like Mac OS X; ru-RU) AppleWebKit/537.36 (KHTML, like Gecko)  Version/11.4.1 Mobile/15G77 Safari/537.36 Puffin/5.2.2IP
      x-csrftoken: 
      x-ig-app-id: 1217981644879628
      cookie: 
BODY
      upload_id=$microtime&caption={, }&usertags=&custom_accessibility_caption=&retry_timeout=
{"status":"ok", "media":{"id":"***", ...}}
就是这样,我在instagram上实现了最小包装。我在github上发布了完整的工作版本。
我将很高兴听到有关实施的消息,也许是我做错了。