' . nl2br(preg_replace($pattern, $replace, htmlentities($textContent)));
$item['enclosures'] = [$mediaURI];
break;
case 'GraphVideo':
$data = $this->getInstagramVideoData($item['uri'], $mediaURI, $media, $textContent);
$item['content'] = $data[0];
if ($directLink) {
$item['enclosures'] = $data[1];
} else {
$item['enclosures'] = [$mediaURI];
}
$item['thumbnail'] = $mediaURI;
break;
default:
break;
}
$item['timestamp'] = $media->taken_at_timestamp;
$this->items[] = $item;
}
}
// returns Sidecar(a post which has multiple media)'s contents and enclosures
protected function getInstagramSidecarData($uri, $postTitle, $mediaInfo, $textContent)
{
$enclosures = [];
$content = '';
foreach ($mediaInfo->edge_sidecar_to_children->edges as $singleMedia) {
$singleMedia = $singleMedia->node;
if ($singleMedia->is_video) {
if (in_array($singleMedia->video_url, $enclosures)) {
continue; // check if not added yet
}
$content .= ' ';
array_push($enclosures, $singleMedia->video_url);
} else {
if (in_array($singleMedia->display_url, $enclosures)) {
continue; // check if not added yet
}
$content .= '';
$content .= '';
$content .= ' ';
array_push($enclosures, $singleMedia->display_url);
}
}
$content .= ' ' . nl2br(htmlentities($textContent));
return [$content, $enclosures];
}
// returns Video post's contents and enclosures
protected function getInstagramVideoData($uri, $mediaURI, $mediaInfo, $textContent)
{
$content = ' ';
$content .= ' ' . nl2br(htmlentities($textContent));
return [$content, [$mediaInfo->video_url]];
}
protected function getTextContent($media)
{
$textContent = '(no text)';
//Process the first element, that isn't in the node graph
if (count($media->edge_media_to_caption->edges) > 0) {
$textContent = trim($media->edge_media_to_caption->edges[0]->node->text);
}
return $textContent;
}
protected function getInstagramJSON($uri)
{
// Sets fallbackMode to false
$this->fallbackMode = false;
if (!is_null($this->getInput('u'))) {
try {
$userId = $this->getInstagramUserId($this->getInput('u'));
// If the Userid is not null, try to load the data from the graphql
if (!$userId) {
$data = $this->getContents(self::URI .
'graphql/query/?query_hash=' .
self::USER_QUERY_HASH .
'&variables={"id"%3A"' .
$userId .
'"%2C"first"%3A10}');
} else {
// In case we did not get the UserId then we must go back to the fallback mode
$data = $this->getInstagramJSONFallback();
}
} catch (HttpException $e) {
// Even if the UserId is not nul, the graphql request could go wrong, and then we should try to use the fallback mode
$data = $this->getInstagramJSONFallback();
}
return json_decode($data);
} elseif (!is_null($this->getInput('h'))) {
$data = $this->getContents(self::URI .
'graphql/query/?query_hash=' .
self::TAG_QUERY_HASH .
'&variables={"tag_name"%3A"' .
$this->getInput('h') .
'"%2C"first"%3A10}');
return json_decode($data);
} else {
$html = getContents($uri);
$scriptRegex = '/window\._sharedData = (.*);<\/script>/';
$ret = preg_match($scriptRegex, $html, $matches, PREG_OFFSET_CAPTURE);
if ($ret) {
return json_decode($matches[1][0]);
}
return null;
}
}
protected function getInstagramJSONFallback()
{
// If loading the data directly failed, we fall back to the "/embed" data loading
// We are in the fallback mode : set a booolean to handle this specific case while collecting the content
$this->fallbackMode = true;
// Get the HTML code of the profile embed page, and extract the JSON of it
$username = $this->getInput('u');
// Load the content using the integrated function to use helping headers
$htmlString = $this->getContents(self::URI . $username . '/embed/');
// Load the String as an SimpleHTMLDom Object
$html = new simple_html_dom();
$html->load($htmlString);
// Find the