Skip to content

Commit 233bd80

Browse files
authoredJul 24, 2023
Merge pull request #1189 from abraham/media_id
Check Twitter response in uploadMediaChunked
2 parents 5188963 + 13ed24c commit 233bd80

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed
 

‎src/TwitterOAuth.php

+4
Original file line numberDiff line numberDiff line change
@@ -364,13 +364,17 @@ private function uploadMediaNotChunked(string $path, array $parameters)
364364
*/
365365
private function uploadMediaChunked(string $path, array $parameters)
366366
{
367+
/** @var object $init */
367368
$init = $this->http(
368369
'POST',
369370
self::UPLOAD_HOST,
370371
$path,
371372
$this->mediaInitParameters($parameters),
372373
false,
373374
);
375+
if (!property_exists($init, 'media_id_string')) {
376+
throw new TwitterOAuthException('Missing "media_id_string"');
377+
}
374378
// Append
375379
$segmentIndex = 0;
376380
$media = fopen($parameters['media'], 'rb');

‎tests/TwitterOAuthMediaTest.php

+18
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,22 @@ public function testPostStatusesUpdateWithMediaChunked()
8888
$result = $this->twitter->post('statuses/destroy/' . $result->id_str);
8989
return $result;
9090
}
91+
92+
/**
93+
* @vcr testPostStatusesUpdateWithMediaChunkedException.json
94+
*/
95+
public function testPostStatusesUpdateWithMediaChunkedException()
96+
{
97+
$this->expectException(
98+
\Abraham\TwitterOAuth\TwitterOAuthException::class,
99+
);
100+
$this->expectErrorMessage('Missing "media_id_string"');
101+
// Video source http://www.sample-videos.com/
102+
$file_path = __DIR__ . '/video.mp4';
103+
$result = $this->twitter->upload(
104+
'media/upload',
105+
['media' => $file_path, 'media_type' => 'video/mp4'],
106+
true,
107+
);
108+
}
91109
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
[{
2+
"request": {
3+
"method": "POST",
4+
"url": "https:\/\/upload.twitter.com\/1.1\/media\/upload.json",
5+
"headers": {
6+
"Host": "upload.twitter.com",
7+
"Accept": "application\/json",
8+
"Authorization": "OAuth oauth_version=\"1.0\", oauth_nonce=\"2b67ebbeace76543f356ba8bbd59abde\", oauth_timestamp=\"1587861062\", oauth_consumer_key=\"awJfND4zFGapGOFKfdjg\", oauth_token=\"93915746-KjE3c27dCt8awONxuUAaJ00yishXXwcH5CdLBnO1x\", oauth_signature_method=\"HMAC-SHA1\", oauth_signature=\"cOKZmxZ5f3bxiwWU%2B1cJ9hWpUL4%3D\"",
9+
"Expect": null
10+
},
11+
"body": "command=INIT&media_type=video%2Fmp4&total_bytes=383631"
12+
},
13+
"response": {
14+
"status": {
15+
"http_version": "2",
16+
"code": "400",
17+
"message": ""
18+
},
19+
"headers": {
20+
"cache-control": "no-cache, no-store, must-revalidate, pre-check=0, post-check=0",
21+
"content-disposition": "attachment; filename=json.json",
22+
"content-encoding": "gzip",
23+
"content-length": "101",
24+
"content-type": "application\/json;charset=utf-8",
25+
"date": "Sun, 26 Apr 2020 00:31:54 GMT",
26+
"expires": "Tue, 31 Mar 1981 05:00:00 GMT",
27+
"last-modified": "Sun, 26 Apr 2020 00:31:54 GMT",
28+
"pragma": "no-cache",
29+
"server": "tsa_b",
30+
"set-cookie": "personalization_id=\"v1_0CYjFmw6Rjdl\/xKmqvzf4g==\"; Max-Age=63072000; Expires=Tue, 26 Apr 2022 00:31:54 GMT; Path=\/; Domain=.twitter.com; Secure; SameSite=None, lang=en; Path=\/, guest_id=v1%3A158786111466374311; Max-Age=63072000; Expires=Tue, 26 Apr 2022 00:31:54 GMT; Path=\/; Domain=.twitter.com; Secure; SameSite=None",
31+
"status": "202 Accepted",
32+
"strict-transport-security": "max-age=631138519",
33+
"vary": "Origin",
34+
"x-access-level": "read-write-directmessages",
35+
"x-connection-hash": "448b96791c0d13223e24f9c1edc4a7fa",
36+
"x-frame-options": "SAMEORIGIN",
37+
"x-mediaid": "1254206535166763008",
38+
"x-rate-limit-limit": "200",
39+
"x-rate-limit-remaining": "198",
40+
"x-rate-limit-reset": "1587864355",
41+
"x-response-time": "28",
42+
"x-transaction": "009da55c002c6fca",
43+
"x-tsa-request-body-time": "1",
44+
"x-twitter-response-tags": "BouncerCompliant",
45+
"x-xss-protection": "1; mode=block"
46+
},
47+
"body": "{\"errors\":[{\"code\":324,\"message\":\"Failed to process media.\"}]}"
48+
}
49+
}]

0 commit comments

Comments
 (0)
Please sign in to comment.