
在使用bing新闻搜索api时,开发者常希望获取新闻条目的原始图片,而非仅限于缩略图。为此,api提供了originalimg参数。然而,许多开发者在使用此参数时发现其并未按预期工作,导致返回结果中图片信息仅包含缩略图url,而缺少原始图片url。本文将深入探讨这一问题,并提供正确的解决方案。
当开发者尝试通过Bing新闻搜索API获取新闻图片,并在请求中设置originalImg=true时,如果使用了/news端点,API的响应中通常只包含图片的缩略图信息,例如thumbnail.contentUrl,而缺少直接指向原始图片尺寸的contentUrl字段。
以下是一个典型的错误使用示例及其对应的响应结构:
错误用法示例代码:
const url = 'https://bing-news-search1.p.rapidapi.com/news?&originalImg=true&category=india&cc=in&safeSearch=Off&textFormat=Raw';
const options = {
method: 'GET',
headers: {
'X-BingApis-SDK': 'true',
'X-RapidAPI-Key': 'YOUR_RAPIDAPI_KEY', // 请替换为您的RapidAPI密钥
'X-RapidAPI-Host': 'bing-news-search1.p.rapidapi.com'
}
};
fetch(url, options)
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error(error));上述请求的典型响应片段(仅包含缩略图):
{
"url": "https://www.msn.com/en-in/news/world/...",
"image": {
"_type": "ImageObject",
"thumbnail": {
"_type": "ImageObject",
"contentUrl": "https://www.bing.com/th?id=OVFT._MJPdPJ958_nyj_Ker8qOy&pid=News",
"width": 1280,
"height": 720,
"isLicensed": true
}
}
}从上述响应可以看出,image对象中只有thumbnail字段提供了图片URL,且该URL指向的是Bing处理过的缩略图,而非原始尺寸图片。开发者期望的是在image对象下直接获得一个contentUrl字段,指向原始图片,类似如下结构:
{
"image": {
"_type": "ImageObject",
"contentUrl": "https://s.yimg.com/ny/api/res/1.2/...", // 期望的原始图片URL
"thumbnail": {
"_type": "ImageObject",
"contentUrl": "https://www.bing.com/th?id=OVFT.1Wypj4IiKxycYuiB2Q81GC&pid=News",
"width": 700,
"height": 350
}
}
}根据Bing新闻搜索API v7的官方文档说明,originalImg参数并非对所有新闻相关的API端点都有效。它明确指出:
"Use this parameter only with the News Search API. Do not specify this parameter when calling the Web Search API. Trending Topics ignores this parameter."
这意味着originalImg参数仅适用于/news/search端点,而当使用/news(获取热门新闻或特定类别新闻)或Trending Topics等其他端点时,该参数会被忽略。上述问题中,开发者使用了/news端点,因此originalImg=true的设置未能生效。
要成功获取新闻的原始图片URL,开发者必须将请求的目标端点从/news更改为/news/search。通过/news/search端点,并设置originalImg=true,API将会在响应的image对象中包含contentUrl字段,指向原始尺寸的图片。
正确用法示例代码:
const url = 'https://bing-news-search1.p.rapidapi.com/news/search?q=india news&originalImg=true&category=india&cc=in&safeSearch=Off&textFormat=Raw'; // 注意端点已更改为 /news/search
const options = {
method: 'GET',
headers: {
'X-BingApis-SDK': 'true',
'X-RapidAPI-Key': 'YOUR_RAPIDAPI_KEY', // 请替换为您的RapidAPI密钥
'X-RapidAPI-Host': 'bing-news-search1.p.rapidapi.com'
}
};
fetch(url, options)
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error(error));上述请求的预期响应片段(包含原始图片URL):
{
"value": [
{
"url": "https://www.msn.com/en-in/news/world/...",
"image": {
"_type": "ImageObject",
"contentUrl": "https://media.zenfs.com/en/hearst_womens_health_52/9743c154e3be883ec18c41f03fc44d58", // 原始图片URL
"thumbnail": {
"_type": "ImageObject",
"contentUrl": "https://www.bing.com/th?id=OVFT.1Wypj4IiKxycYuiB2Q81GC&pid=News",
"width": 700,
"height": 350
}
},
// ... 其他新闻条目信息
}
]
}在上述正确用法中,url中的端点已从/news修改为/news/search,并且通常需要提供一个q(查询词)参数来指定搜索内容。此时,API响应的image对象中便会包含contentUrl字段,指向原始尺寸的图片。
通过理解originalImg参数的端点限制,开发者可以更精确地控制Bing新闻搜索API的行为,从而有效地获取所需的原始图片信息,提升应用程序的功能性和用户体验。
以上就是解决Bing新闻搜索API中originalImg参数不生效的问题的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号