From 1e7545ec2df2c0532d6bd7255cb60d269a5ed113 Mon Sep 17 00:00:00 2001 From: Ricdijk Date: Mon, 28 Dec 2020 20:30:42 +0100 Subject: [PATCH] implemented media::hasLiked() and added function to example likeAndUnlikeMdeia.php (#835) --- examples/likeAndUnlikeMedia.php | 36 +++++++++++++++++++++++++--- src/InstagramScraper/Model/Media.php | 18 +++++++++++++- 2 files changed, 50 insertions(+), 4 deletions(-) diff --git a/examples/likeAndUnlikeMedia.php b/examples/likeAndUnlikeMedia.php index 99cfc54f..412e799d 100644 --- a/examples/likeAndUnlikeMedia.php +++ b/examples/likeAndUnlikeMedia.php @@ -1,15 +1,45 @@ login(); +$mediaId='2474033634010898853'; + +function loghasLiked($bool) +{ + echo "HasLiked: "; + var_export($bool); + if ($bool === null) echo " -> Unknown\n
"; //e.g. getMedia by tag won't give us haslike (-> an additional call to getMediaByCode/Id will give the result) + elseif ($bool) echo " -> Liked\n
"; + else echo " -> Not Liked\n
"; +} + try { - $instagram->like('1663256735663694497'); - $instagram->unlike('1663256735663694497'); + loghasLiked($instagram->getMediaById($mediaId)->getHasLiked()); + //hasLiked: False -> Not Liked + + $instagram->like($mediaId); + loghasLiked($instagram->getMediaById($mediaId)->getHasLiked()); + //hasLiked: True -> Liked + + $instagram->unlike($mediaId); + loghasLiked($instagram->getMediaById($mediaId)->getHasLiked()); + //hasLiked: False -> Not Liked + + echo "\n
"; + $media = $instagram->getCurrentTopMediasByTagName('Photography')[0]; + loghasLiked($media->getHasLiked()); + //hasLiked: NULL -> Unknown + + $media = $instagram->getMediaById($media->getId()); + loghasLiked($media->getHasLiked()); + //hasLiked: False -> Not Liked + } catch (InstagramException $ex) { echo $ex->getMessage(); } diff --git a/src/InstagramScraper/Model/Media.php b/src/InstagramScraper/Model/Media.php index 3c35d8f2..98a196f1 100644 --- a/src/InstagramScraper/Model/Media.php +++ b/src/InstagramScraper/Model/Media.php @@ -125,6 +125,11 @@ class Media extends AbstractModel */ protected $likesCount = 0; + /** + * @var boolean + */ + protected $hasLiked = null; + /** * @var */ @@ -401,6 +406,14 @@ public function getLikesCount() return $this->likesCount; } + /** + * @return boolean + */ + public function getHasLiked() + { + return $this->hasLiked; + } + /** * @return mixed */ @@ -672,6 +685,9 @@ protected function initPropertiesCustom($value, $prop, $arr) case 'edge_liked_by': $this->likesCount = $arr[$prop]['count']; break; + case 'viewer_has_liked': + $this->hasLiked = $arr[$prop]; + break; case 'edge_media_to_caption': if (is_array($arr[$prop]['edges']) && !empty($arr[$prop]['edges'])) { $first_caption = $arr[$prop]['edges'][0]; @@ -774,4 +790,4 @@ public function getOwner() { return $this->owner; } -} \ No newline at end of file +}