php – 将图像/视频发布到Twitter
发布时间:2020-12-13 16:00:14 所属栏目:PHP教程 来源:网络整理
导读:当我尝试使用Twitter API发布图像或视频时,我收到错误消息 {“request”:”/i/media/upload.json”,”error”:”Segments do not add up to provided total file size.”} Google没有关于此错误的任何信息. Twitter文档也没有帮助.怎么了? 我在做什么: 1.
当我尝试使用Twitter API发布图像或视频时,我收到错误消息
Google没有关于此错误的任何信息. Twitter文档也没有帮助.怎么了? 我在做什么: 1. INIT uploading file > $this->SetUrl('upload.twitter.com/i/media/upload.json?command=INIT&media_type=image%2Fpng&total_bytes=' > . $filesize) > ->SendRequest('post'); 2. APPEND chunks > $reply = ['media' => $chunk]; > $this->SetQuery($reply); > $this->SetUrl('upload.twitter.com/i/media/upload.json?command=APPEND&media_id='.$mediaId.'&segment_index='.$segment_id)->SendRequest('post'); 3. When I FINALIZE,I get the error > $this->SetUrl('upload.twitter.com/i/media/upload.json?command=FINALIZE&media_id=' > . $mediaId)->SendRequest('post'); 解决方法// APPEND $ch = curl_init(); $file = fopen($media,'r'); $chunk_id = 0; while (!feof($file)) { $append_headers = array(); $chunk = fread($file,1048576); // 1mb per chunk $boundary = $chunk_id.time(); $params = "--" . $boundary . "rn" . "Content-Type: video/mp4rn" . "Content-Disposition: form-data; name="media"; filename="blob"rn" . "rn" . $chunk . "rn" . "--" . $boundary . "--"; $append_headers[] = 'Content-Length: ' . strlen($params); $append_headers[] = 'Content-Type: multipart/form-data; boundary=' . $boundary; curl_setopt($ch,CURLOPT_HTTPHEADER,$append_headers); curl_setopt($ch,CURLOPT_RETURNTRANSFER,true); curl_setopt($ch,CURLOPT_POST,CURLOPT_POSTFIELDS,$params); curl_setopt($ch,CURLOPT_USERAGENT,$this->curl_config['useragent']); curl_setopt($ch,CURLOPT_COOKIEJAR,$this->cookie_dir . md5($this->UserName . $this->Password)); curl_setopt($ch,CURLOPT_COOKIEFILE,CURLOPT_URL,'https://upload.twitter.com/i/media/upload.json?command=APPEND&media_id=' . $init->media_id . '&segment_index=' . $chunk_id); $response = curl_exec($ch); $chunk_id++; } 这是我发布块的解决方案.效果很好. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |