gpt4 book ai didi

php - curl_exec 在尝试连接到 URL 时返回 false

转载 作者:太空狗 更新时间:2023-10-29 14:53:31 26 4
gpt4 key购买 nike

我正在尝试创建一个推送通知应用程序,它将消息从服​​务器发送到用户设备。

现在我得到了注册 ID,但是当我运行 curl_exec 函数时,我得到以下错误:

Curl failed: couldn't connect to host

我不知道为什么会这样,我做了一些检查,当我尝试将 URL 更改为“google.com”时通过了。如果我尝试从我的浏览器访问原始 URL,它会将我重定向到:“https://developers.google.com/cloud-messaging/”这是 PHP 代码:

    <?php
class GCM {

function __construct() {

}
/**
* Sending Push Notification
*/
public function send_notification($registatoin_ids, $message) {
// include config
include_once 'connection.php';

// Set POST variables
$url = 'https://android.googleapis.com/gcm/send';

$fields = array(
'registration_ids' => $registatoin_ids,
'data' => $message,
);

$headers = array(
'Authorization: key=' .GOOGLE_API_KEY,
'Content-Type: application/json'
);
// Open connection
$ch = curl_init();

// Set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// Disabling SSL Certificate support temporarly
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));

// Execute post
$result = curl_exec($ch);
if ($result === FALSE) {
die('Curl failed: ' . curl_error($ch));
}

// Close connection
curl_close($ch);
echo $result;
}
}
?>

最佳答案

我发现我的服务器包含阻止我传出连接的防火墙。所以我打开它,它现在可以工作了。这是通过相关答案的链接帮助我解决问题的解决方案。

CURL 错误代码 7 (CURLE_COULDNT_CONNECT)非常明确......这意味着无法连接()到主机或代理。

以下代码适用于任何系统:

$ch = curl_init("http://google.com");    // initialize curl handle
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$data = curl_exec($ch);
print($data);

如果你看不到谷歌页面那么..你的 URL 是错误的或者你有一些防火墙或限制问题

this link help me to resolve this issue

关于php - curl_exec 在尝试连接到 URL 时返回 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33372527/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com