链接到 AMP 内容
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
使用 AMP 网址 API 将网址与对应的 AMP 网址进行匹配,并改为投放 AMP 版本。
将网址与 AMP 网址匹配
通过以下方式请求 AMP 网址:
POST https://acceleratedmobilepageurl.googleapis.com/v1/ampUrls:batchGet
在请求正文中列出最多 50 个网址作为载荷:
{"urls": [
"https://www.amp.dev",
"https://www.example.org/article-without-amp-version"
]}
所有请求都需要通过 X-Authorization 标头 (X-Goog-Api-Key: YOUR-DEVELOPER-KEY
) 包含开发密钥。X-Authorization 标头值区分大小写。如果您没有开发密钥,请参阅授权了解设置说明。
使用 curl 的示例:
curl -i -s -k -X POST -H "Content-Type: application/json" -H "X-Goog-Api-Key: " -d "{urls: ['https://example.com']}" "https://acceleratedmobilepageurl.googleapis.com/v1/ampUrls:batchGet"
将网址替换为 AMP 网址
batchGet
方法会返回 JSON 格式的 AMP 网址映射:
{
"ampUrls": [
{
"originalUrl": "https://example.com",
"ampUrl": "https://example.com",
"cdnAmpUrl":
"https://cdn.ampproject.org/c/s/example.com"
}
],
"urlErrors": [
{
"errorCode": "NO_AMP_URL",
"errorMessage": "AMP URL not found.",
"originalUrl": "http://www.example.com/no-amp-version.html"
}
]
}
原始 AMP 网址和存储在 Google AMP Cache 中的网址都会在响应正文中返回。我们建议您使用 "cdnAmpUrl"
(而非 "ampUrl"
),以便您的应用可以享受 Google AMP Cache 的所有优势。
设置重定向
每当您在服务器上移动 AMP 文件的位置时,都需要设置从旧位置到新位置的重定向。在解析 AMP 网址时,Google AMP Cache 会跟踪重定向。
如果网址重定向到另一个有效的 AMP 网址,Google AMP Cache 会返回并缓存已解析的重定向的内容。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2023-08-30。
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["没有我需要的信息","missingTheInformationINeed","thumb-down"],["太复杂/步骤太多","tooComplicatedTooManySteps","thumb-down"],["内容需要更新","outOfDate","thumb-down"],["翻译问题","translationIssue","thumb-down"],["示例/代码问题","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2023-08-30。"],[[["Utilize the AMP URL API to find corresponding AMP URLs and serve those versions to users."],["The API allows matching up to 50 URLs at once by sending a POST request with a JSON payload containing the URLs."],["Include a developer key in the request header for authorization purposes."],["The API response provides both the original AMP URL and the Google AMP Cache URL; prioritize using the cached version (`cdnAmpUrl`)."],["Implement redirects for moved AMP files to ensure proper resolution by the Google AMP Cache."]]],["To obtain AMP URLs, use the AMP URL API's `batchGet` method via a POST request to `acceleratedmobilepageurl.googleapis.com/v1/ampUrls:batchGet`. Include up to 50 URLs in the request body and a developer key in the `X-Goog-Api-Key` header. The response will provide a JSON mapping, including original and CDN AMP URLs. It's recommended to use `\"cdnAmpUrl\"`. Set up redirects for moved AMP files to ensure the Google AMP Cache resolves and caches the correct content.\n"]]