我应如何使用PHP产量法
我正在尝试编写一个API控制器,该控制器可以使用PHP提取用户的统计信息,但是我编写的工作非常缓慢。根据我的其他编程语言经验,我决定尝试使用屈服方法,但是出现错误,我该怎么办?提前致谢。
这是控制器代码
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Http;
class splenperAPIController extends Controller
{
public function splenperAPI()
{
$response = Http::withHeaders([
'cookie' => 'sessionid=1796659686%3A4Ojj1py72bZKql%3A7;csrftoken=fPYrPRD1vHB7LdS0DjKzOK4kGo4uYK9f;',
'x-ig-app-id' => '936619743392459',
])->get('https://www.instagram.com/elmaligroup/?__a=1');
$response = $response->json();
$userId = $response['graphql']['user']['id'];
$first = 12;
$variables = json_encode([
'id' => $userId,
"first" => $first,
]);
$variables = urlencode($variables);
$response = Http::withHeaders([
'cookie' => 'sessionid=1796659686%3A4Ojj1py72bZKql%3A7; csrftoken=fPYrPRD1vHB7LdS0DjKzOK4kGo4uYK9f; ds_user_id=1796659686; ig_did=25114CCD-7A9D-4971-88F1-1E04796D9F14; ig_nrcb=1; mid=YiqZZQALAAEhkff6N5T2oovGOBkz; rur=01f73b7ee19feca3df296fc45ee75179c49dd54efb93233307b58f19bf707fea5d458fe7; shbid=01f7998eadf528233f0e7331b327e5a107134084c64e229af2cc6577b8a2ce862e3798da; shbts=01f7caf607a6877ca8407e9cb85cd175736a87f8f75e9076d5bddb32cbcbac6d5da45e89',
'x-ig-app-id' => '936619743392459',
'Content-Type' => 'application/json',
])->get('https://www.instagram.com/graphql/query/?query_hash=e769aa130647d2354c40ea6a439bfc08&variables=' . $variables);
$mediaCount = $response->json()['data']['user']['edge_owner_to_timeline_media']['count'];
$totalLikeCount = 0;
$totalCommentCount = 0;
for ($i = 0; $i < $mediaCount; $i++) {
yield $totalCommentCount += $response->json()['data']['user']['edge_owner_to_timeline_media']['edges'][$i]['node']['edge_media_to_comment']['count'];
yield $totalLikeCount += $response->json()['data']['user']['edge_owner_to_timeline_media']['edges'][$i]['node']['edge_media_preview_like']['count'];
yield $userName = $response->json()['data']['user']['edge_owner_to_timeline_media']['edges'][$i]['node']['owner']['username'];
}
echo "userId:" . $userId . '<br>';
echo "totalLikeCount:" . $totalLikeCount . '<br>';
echo "totalCommentCount:" . $totalCommentCount . '<br>';
echo "userName:" . $userName . '<br>';
echo "mediaCount:" . $mediaCount;
}
}
这是错误代码
TypeError
Symfony\Component\HttpFoundation\Response::setContent(): Argument #1 ($content) must be of type ?string, Generator given, called in C:\Users\ASUS\Desktop\splenperAPI\vendor\laravel\framework\src\Illuminate\Http\Response.php on line 72
I'm trying to write an api controller that pulls the user's statistics with php, but this controller I wrote works very slowly. Based on my other programming language experiences, I decided to try the yield method, but an error appears, what is the reason and what should I do? Thanks in advance.
this is controller code
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Http;
class splenperAPIController extends Controller
{
public function splenperAPI()
{
$response = Http::withHeaders([
'cookie' => 'sessionid=1796659686%3A4Ojj1py72bZKql%3A7;csrftoken=fPYrPRD1vHB7LdS0DjKzOK4kGo4uYK9f;',
'x-ig-app-id' => '936619743392459',
])->get('https://www.instagram.com/elmaligroup/?__a=1');
$response = $response->json();
$userId = $response['graphql']['user']['id'];
$first = 12;
$variables = json_encode([
'id' => $userId,
"first" => $first,
]);
$variables = urlencode($variables);
$response = Http::withHeaders([
'cookie' => 'sessionid=1796659686%3A4Ojj1py72bZKql%3A7; csrftoken=fPYrPRD1vHB7LdS0DjKzOK4kGo4uYK9f; ds_user_id=1796659686; ig_did=25114CCD-7A9D-4971-88F1-1E04796D9F14; ig_nrcb=1; mid=YiqZZQALAAEhkff6N5T2oovGOBkz; rur=01f73b7ee19feca3df296fc45ee75179c49dd54efb93233307b58f19bf707fea5d458fe7; shbid=01f7998eadf528233f0e7331b327e5a107134084c64e229af2cc6577b8a2ce862e3798da; shbts=01f7caf607a6877ca8407e9cb85cd175736a87f8f75e9076d5bddb32cbcbac6d5da45e89',
'x-ig-app-id' => '936619743392459',
'Content-Type' => 'application/json',
])->get('https://www.instagram.com/graphql/query/?query_hash=e769aa130647d2354c40ea6a439bfc08&variables=' . $variables);
$mediaCount = $response->json()['data']['user']['edge_owner_to_timeline_media']['count'];
$totalLikeCount = 0;
$totalCommentCount = 0;
for ($i = 0; $i < $mediaCount; $i++) {
yield $totalCommentCount += $response->json()['data']['user']['edge_owner_to_timeline_media']['edges'][$i]['node']['edge_media_to_comment']['count'];
yield $totalLikeCount += $response->json()['data']['user']['edge_owner_to_timeline_media']['edges'][$i]['node']['edge_media_preview_like']['count'];
yield $userName = $response->json()['data']['user']['edge_owner_to_timeline_media']['edges'][$i]['node']['owner']['username'];
}
echo "userId:" . $userId . '<br>';
echo "totalLikeCount:" . $totalLikeCount . '<br>';
echo "totalCommentCount:" . $totalCommentCount . '<br>';
echo "userName:" . $userName . '<br>';
echo "mediaCount:" . $mediaCount;
}
}
this is error code
TypeError
Symfony\Component\HttpFoundation\Response::setContent(): Argument #1 ($content) must be of type ?string, Generator given, called in C:\Users\ASUS\Desktop\splenperAPI\vendor\laravel\framework\src\Illuminate\Http\Response.php on line 72
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论