不在对象上下文中时使用 $this 时出错
Error Using $this when not in object context
首先post在这里,如果格式不正确,请提前致歉。我正在使用 Instagram API 来提取图像。 Instagram API 一次只有 returns 一页图像,但提供分页和 next_url 以抓取下一页图像。当我使用下面的函数 fetchInstagramAPI 时,只抓取第一页,php 代码工作正常。
当我尝试将 loopPages 函数与 fetchInstagramAPI 函数一起使用以尝试一次抓取所有页面时,我收到错误 "Using $this when not in object context"。任何想法?预先感谢您的帮助。
函数 fetchInstagramAPI 获取我们的数据
<?php
function fetchInstagramAPI($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
$contents = curl_exec($ch);
curl_close($ch);
return json_decode($contents);
}
函数 loopPages 使用分页和 next_url 抓取图像的所有页面
function loopPages($url){
$gotAllResults = false;
$results = array();
while(!$gotAllResults) {
$result = $this->fetchInstagramAPI($url);
$results[] = $result;
if (!property_exists($result->pagination, 'next_url')) {
$gotAllResults = true;
} else {
$url = $result->pagination->next_url;
}
}
return $results;
}
这会拉取、解析图像,然后在浏览器中显示图像
$all_url = 'https://api.instagram.com/v1/users/{$userid}/media/recent/?client_id={$clientid}';
$media = loopPages($all_url);
foreach ($media->data as $post): ?>
<!-- Renders images. @Options (thumbnail, low_resoulution, standard_resolution) -->
<a class="group" rel="group1" href="<?= $post->images->standard_resolution->url ?>"><img src="<?= $post->images->thumbnail->url ?>"></a>
<?php endforeach ?>
在PHP和许多面向对象的语言中$this
是对当前对象(或调用对象)的引用。因为你的代码似乎不存在 class $this
不存在。检查 this link 是否有 PHP classes 和对象。
由于您刚刚在文件中定义了函数,因此您可以尝试使用 $result = fetchInstagramAPI($url);
(不使用 $this
)调用函数。
编辑:
foreach 检查 $media->data
是否实际上是一个数组并尝试另一种我认为更容易阅读的语法。
edit2:
既然您现在知道了您的 $media
看起来如何,您可以绕过另一个将遍历页面的 foreach 循环:
foreach ($media as $page){
foreach ($page->data as $post) {
echo '<!-- Renders images. @Options (thumbnail, low_resoulution, standard_resolution) -->';
echo '<a class="group" rel="group1" href="' . $post->images->standard_resolution->url . '"><img src="' . $post->images->thumbnail->url . '"></a>';
}
}
首先post在这里,如果格式不正确,请提前致歉。我正在使用 Instagram API 来提取图像。 Instagram API 一次只有 returns 一页图像,但提供分页和 next_url 以抓取下一页图像。当我使用下面的函数 fetchInstagramAPI 时,只抓取第一页,php 代码工作正常。
当我尝试将 loopPages 函数与 fetchInstagramAPI 函数一起使用以尝试一次抓取所有页面时,我收到错误 "Using $this when not in object context"。任何想法?预先感谢您的帮助。
函数 fetchInstagramAPI 获取我们的数据
<?php
function fetchInstagramAPI($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
$contents = curl_exec($ch);
curl_close($ch);
return json_decode($contents);
}
函数 loopPages 使用分页和 next_url 抓取图像的所有页面
function loopPages($url){
$gotAllResults = false;
$results = array();
while(!$gotAllResults) {
$result = $this->fetchInstagramAPI($url);
$results[] = $result;
if (!property_exists($result->pagination, 'next_url')) {
$gotAllResults = true;
} else {
$url = $result->pagination->next_url;
}
}
return $results;
}
这会拉取、解析图像,然后在浏览器中显示图像
$all_url = 'https://api.instagram.com/v1/users/{$userid}/media/recent/?client_id={$clientid}';
$media = loopPages($all_url);
foreach ($media->data as $post): ?>
<!-- Renders images. @Options (thumbnail, low_resoulution, standard_resolution) -->
<a class="group" rel="group1" href="<?= $post->images->standard_resolution->url ?>"><img src="<?= $post->images->thumbnail->url ?>"></a>
<?php endforeach ?>
在PHP和许多面向对象的语言中$this
是对当前对象(或调用对象)的引用。因为你的代码似乎不存在 class $this
不存在。检查 this link 是否有 PHP classes 和对象。
由于您刚刚在文件中定义了函数,因此您可以尝试使用 $result = fetchInstagramAPI($url);
(不使用 $this
)调用函数。
编辑:
foreach 检查 $media->data
是否实际上是一个数组并尝试另一种我认为更容易阅读的语法。
edit2:
既然您现在知道了您的 $media
看起来如何,您可以绕过另一个将遍历页面的 foreach 循环:
foreach ($media as $page){
foreach ($page->data as $post) {
echo '<!-- Renders images. @Options (thumbnail, low_resoulution, standard_resolution) -->';
echo '<a class="group" rel="group1" href="' . $post->images->standard_resolution->url . '"><img src="' . $post->images->thumbnail->url . '"></a>';
}
}