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

用于创建Apple News文章的可用PHP代码及问题排查指引

亲测可用的Apple News文章创建PHP代码及排错心得

我折腾了好一阵才搞定Apple News文章的创建,下面这段PHP代码是我亲测有效的,分享给大家:

<?php
// 配置Apple News API信息
$apiKey = 'YOUR_API_KEY';
$apiSecret = 'YOUR_API_SECRET';
$endpoint = 'https://news-api.apple.com/channels/YOUR_CHANNEL_ID/articles';

// 构建文章内容
$article = [
    'identifier' => 'unique-article-id',
    'title' => '测试Apple News文章',
    'subtitle' => '这是一篇测试文章的副标题',
    'body' => [
        [
            'role' => 'body',
            'text' => '这里是文章的正文内容,支持Apple News格式的富文本。'
        ]
    ],
    'language' => 'zh-CN',
    'layout' => [
        'columns' => 7,
        'width' => 1024
    ]
];

// 生成签名
$timestamp = time();
$signature = hash_hmac('sha256', $timestamp . $endpoint, $apiSecret);

// 发起请求
$ch = curl_init($endpoint);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($article));
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'Content-Type: application/json',
    "Authorization: HHMAC; key=$apiKey; signature=$signature; timestamp=$timestamp"
]);

$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

// 处理响应
if ($httpCode === 201) {
    echo "文章创建成功!";
    var_dump(json_decode($response, true));
} else {
    echo "创建失败,错误码:$httpCode";
    var_dump(json_decode($response, true));
}
?>

排错经验分享

这段代码我自己跑通了,希望能帮到大家少踩坑:

  • 我之前遇到好几个莫名其妙的错误,后来发现一定要去查Apple News官方的错误代码列表,看返回的具体错误编号,就能精准定位问题(比如签名不对、字段缺失、格式错误这些)
  • 官方的示例看起来确实有点模糊,但里面藏着很多关键的配置细节,比如文章的结构、布局参数,一定要严格照着示例来调整,别自己瞎改格式

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 08:11:28