-
-
Notifications
You must be signed in to change notification settings - Fork 140
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add method to post message on a media (refs to #243)
- Loading branch information
Showing
7 changed files
with
179 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Instagram\Api; | ||
use Instagram\Exception\InstagramException; | ||
|
||
use Psr\Cache\CacheException; | ||
use Symfony\Component\Cache\Adapter\FilesystemAdapter; | ||
|
||
require realpath(dirname(__FILE__)) . '/../vendor/autoload.php'; | ||
$credentials = include_once realpath(dirname(__FILE__)) . '/credentials.php'; | ||
|
||
$cachePool = new FilesystemAdapter('Instagram', 0, __DIR__ . '/../cache'); | ||
|
||
try { | ||
$api = new Api($cachePool); | ||
$api->login($credentials->getLogin(), $credentials->getPassword()); | ||
|
||
// 2657627124756968810 is id of https://www.instagram.com/p/CThycoMlPVq/ | ||
$postId = 2657627124756968810; | ||
|
||
$api->commentPost($postId, 'Awesome!'); | ||
} catch (InstagramException $e) { | ||
print_r($e->getMessage()); | ||
} catch (CacheException $e) { | ||
print_r($e->getMessage()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Instagram\Transport; | ||
|
||
use Instagram\Exception\InstagramFetchException; | ||
use Instagram\Utils\Endpoints; | ||
|
||
class CommentPost extends AbstractDataFeed | ||
{ | ||
/** | ||
* @param int $postId | ||
* @param string $message | ||
* | ||
* @return string | ||
* | ||
* @throws InstagramFetchException | ||
*/ | ||
public function comment(int $postId, string $message): string | ||
{ | ||
$endpoint = Endpoints::getCommentUrl($postId); | ||
return $this->fetchData($endpoint, $message); | ||
} | ||
|
||
/** | ||
* @param string $endpoint | ||
* @param string $message | ||
* | ||
* @return string | ||
* | ||
* @throws InstagramFetchException | ||
*/ | ||
private function fetchData(string $endpoint, string $message): string | ||
{ | ||
$data = $this->postJsonDataFeed($endpoint, [ | ||
'comment_text' => $message, | ||
'replied_to_comment_id' => '', | ||
]); | ||
|
||
if (!$data->status) { | ||
throw new InstagramFetchException('Whoops, looks like something went wrong!'); | ||
} | ||
|
||
return $data->status; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
namespace Instagram\Tests; | ||
|
||
use GuzzleHttp\Client; | ||
use GuzzleHttp\Handler\MockHandler; | ||
use GuzzleHttp\HandlerStack; | ||
use GuzzleHttp\Psr7\Response; | ||
use Instagram\Api; | ||
use Instagram\Exception\InstagramFetchException; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class PostCommentTest extends TestCase | ||
{ | ||
use GenerateCookiesTrait; | ||
|
||
public function testPostComment() | ||
{ | ||
$mock = new MockHandler([ | ||
new Response(200, ['Set-Cookie' => 'cookie'], file_get_contents(__DIR__ . '/fixtures/home.html')), | ||
new Response(200, [], file_get_contents(__DIR__ . '/fixtures/login-success.json')), | ||
new Response(200, ['Set-Cookie' => 'cookie'], file_get_contents(__DIR__ . '/fixtures/home.html')), | ||
new Response(200, [], file_get_contents(__DIR__ . '/fixtures/comment-post.json')), | ||
]); | ||
|
||
$handlerStack = HandlerStack::create($mock); | ||
$client = new Client(['handler' => $handlerStack]); | ||
|
||
$api = new Api($this->generateCookiesForFollow(), $client); | ||
|
||
$api->login('username', 'password'); | ||
|
||
$result = $api->commentPost(123455, 'Awesome!'); | ||
$this->assertSame('ok', $result); | ||
|
||
$api->logout('username'); | ||
} | ||
|
||
public function testPostCommentWithError() | ||
{ | ||
$this->expectException(InstagramFetchException::class); | ||
|
||
$mock = new MockHandler([ | ||
new Response(200, ['Set-Cookie' => 'cookie'], file_get_contents(__DIR__ . '/fixtures/home.html')), | ||
new Response(200, [], file_get_contents(__DIR__ . '/fixtures/login-success.json')), | ||
new Response(200, ['Set-Cookie' => 'cookie'], file_get_contents(__DIR__ . '/fixtures/home.html')), | ||
new Response(200, [], '{"status":""}'), | ||
]); | ||
|
||
$handlerStack = HandlerStack::create($mock); | ||
$client = new Client(['handler' => $handlerStack]); | ||
|
||
$api = new Api($this->generateCookiesForFollow(), $client); | ||
|
||
$api->login('username', 'password'); | ||
|
||
$api->commentPost(123455, 'Awesome!'); | ||
|
||
$api->logout('username'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"id":"18091595578284217","from":{"id":"35781928511","username":"rudigerempira","full_name":"Rudiger E.","profile_picture":"https://scontent-arn2-1.cdninstagram.com/v/t51.2885-19/44884218_345707102882519_2446069589734326272_n.jpg?_nc_ht=scontent-arn2-1.cdninstagram.com\u0026_nc_ohc=lcvyX5vEjaAAX8Nk11U\u0026edm=ABmJApABAAAA\u0026ccb=7-4\u0026oh=76314cdf5c3265fe625c55b6865364b7\u0026oe=614F2ECF\u0026_nc_sid=6136e7\u0026ig_cache_key=YW5vbnltb3VzX3Byb2ZpbGVfcGlj.2-ccb7-4"},"text":"Awesome!","created_time":1632144927,"status":"ok"} |