【问题标题】:Undefined constant http/2未定义常量 http/2
【发布时间】:2021-06-03 17:16:58
【问题描述】:

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

我的新脚本是这样的:

<?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,我发现了这个:

【问题讨论】:

    标签: php curl push-notification


    【解决方案1】:

    来自documentation

    CURL_HTTP_VERSION_2_0 (int)
    自 cURL 7.33.0 起可用

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

    【讨论】:

    • 它说:cURL Information 7.29.0 这一定是出现错误的原因。如何将其更新到 7.33?
    • 这取决于您对相关服务器的访问/权限类型。它的范围从要求托管支持自己做(例如像这样serverfault.com/questions/321321/…)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-17
    相关资源
    最近更新 更多