当前位置: 首页 > 工具软件 > Apns-PHP > 使用案例 >

php 使用ios的apns进行推送

郜修雅
2023-12-01
$data = array(
            "aps"=>array(
                'alert'=>'这是推送标题',
                "sound"=>"default",
                "badge"=>0,
            ),
            'app'=>array(
                "title"=>"这是展示标题内容",
                "content"=>"这是自定义内容",
            ),
        );

ini_set('display_errors', 1);
$service_url = 'https://api.sandbox.push.apple.com/3/device/';
$token = '';//ios端获取的token值
// $headers = array();
$apns_topic = ''; // APP的bundle-id
$payload = json_encode($data);
$ch = curl_init($service_url);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            "apns-topic: $apns_topic",
        ));
curl_setopt($ch, CURLOPT_SSLCERT, '.pem'); //推送证书
curl_setopt($ch, CURLOPT_SSLCERTPASSWD, ''); // 证书密码
curl_setopt($ch, CURLOPT_URL, $service_url . $token);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_HEADER, TRUE); // 显示response头
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$result = curl_exec($ch);
$errno = curl_errno($ch);
if ($errno) {
    exit("cURL errno:{$errno}, error:" . curl_error($ch));
}
$responseCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
print_r($responseCode);

如果遇到推送后返回
cURL errno:60, error:SSL certificate problem: unable to get local issuer certificate

解决方法
访问 https://curl.haxx.se/docs/caextract.html,下载cacert.pem,并在php.ini文件 取消openssl.cafile注释 并在后面添加路径添加
openssl.cafile=“C:…\cacert.pem”

 类似资料: