You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel调用YouTube API v3搜索时遇空Referer被拦截问题求助

解决YouTube API v3空Referer请求被阻止的问题

你的curl请求因为缺少Referer请求头被YouTube API拦截了,只需在curl配置中添加合法的Referer字段即可解决。

修改后的代码如下:

$params = array(
    "part" => "snippet",
    "q" => $term,
    "type" => "channel",
    "maxResults" => $limit,
    "key" => env('YOUTUBE_KEY')
);
$curl_session = curl_init();
$curl_options = array(
    CURLOPT_URL => 'https://www.googleapis.com/youtube/v3/search?'. http_build_query($params),
    CURLOPT_RETURNTRANSFER => true,
    // 添加Referer头,使用你的应用域名
    CURLOPT_REFERER => env('APP_URL')
);
curl_setopt_array($curl_session, $curl_options);

$response = curl_exec($curl_session);

curl_close($curl_session);

$response = json_decode($response, true);

return $response; 

补充说明

YouTube API的安全机制会验证请求来源,空Referer会被判定为不安全请求。你可以直接写死应用域名(比如https://your-app-domain.com),或者用Laravel配置的APP_URL环境变量,确保.env文件中APP_URL的值是正确的应用地址。

内容的提问来源于stack exchange,提问作者Shekhar Ghimire

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.29 08:49:56