首页 > 解决方案 > 未定义常量 http/2

问题描述

我正在尝试研究如何从 php 站点向苹果 iOS 设备发送推送通知。我有一个在新的 http/2 方式之前工作的 php 脚本,但由于他们不再支持它,它当然不起作用。

我的新脚本是这样的:

<?php


    $ch = curl_init();
    $device_token   = 'correctDevice';
    $pem_file       = 'correctPemfile';
    $pem_secret     = 'correctCode';
    $apns_topic     = 'correctAppID';

    //curl_setopt($ch, CURLOPT_SSLVERSION, 6);
    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_file);
    curl_setopt($ch, CURLOPT_SSLCERTPASSWD, $pem_secret);
    curl_setopt($ch, CURLOPT_VERBOSE , true);

    echo "Try 1 ================================================" . PHP_EOL;

    //setup and send first push message
    $url = "https://api.development.push.apple.com/3/device/$device_token";
    curl_setopt($ch, CURLOPT_URL, "{$url}");
    $sample_alert = '{"aps":{"alert":"hi #1","sound":"default"}}';
    curl_setopt($ch, CURLOPT_POSTFIELDS, $sample_alert);

    $response = curl_exec($ch);
    $httpcode = curl_getinfo($ch);
    //var_dump($response);
    //var_dump($httpcode);

    echo "Try 2 ================================================" . PHP_EOL;

    //setup and send second push message
    $url = "https://api.development.push.apple.com/3/device/$device_token";
    curl_setopt($ch, CURLOPT_URL, "{$url}");
    $sample_alert = '{"aps":{"alert":"hi #2","sound":"default"}}';
    curl_setopt($ch, CURLOPT_POSTFIELDS, $sample_alert);

    $response = curl_exec($ch);
    $httpcode = curl_getinfo($ch);
    //var_dump($response);
    //var_dump($httpcode);

    curl_close($ch);
    

?>

当然,我已经屏蔽了设备令牌、pem 文件、秘密和应用程序 ID,但它们都是正确的。

问题是我收到此错误: Warning: Use of undefined constant CURL_HTTP_VERSION_2_0 - assumed 'CURL_HTTP_VERSION_2_0' (this will throw an Error in a future version of PHP)

这个问题可能是什么?我尝试回显phpinfo(),看看它是否支持 http/2,我发现了这个: 在此处输入图像描述

标签: phpcurlpush-notification

解决方案


文档中:

CURL_HTTP_VERSION_2_0 (int)
自 cURL 7.33.0 起可用

检查 cURL 版本(例如使用phpinfo())。


推荐阅读