gpt4 book ai didi

php - 在继续之前等待 curl 完成?

转载 作者:行者123 更新时间:2023-12-05 07:42:26 24 4
gpt4 key购买 nike

我正在使用脚本通过 curl 将日期发送到网上商店。在同一个脚本中,执行此脚本时会发送一封邮件。邮件通常在 curl 完成之前发送,因此缺少关键参数。如何更改脚本以便在执行 curl 后发送邮件?

// Some more curl code here

curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);

// execute post
$result = curl_exec($ch);

$data = json_decode($result);

// close connection
curl_close($ch);

// send mail
require('assets/phpmailer/class.phpmailer.php');

$mail = new phpmailer();
$mail->IsHTML(true);

// Some more mail code here

最佳答案

您应该检查 curl 响应

if (curl_errno($ch)) {
// this would be your first hint that something went wrong
die('Couldn\'t send request: ' . curl_error($ch));
} else {
// check the HTTP status code of the request
$resultStatus = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($resultStatus != 200) {
die('Request failed: HTTP status code: ' . $resultStatus);
}
else{
//send the mail
}
}

关于php - 在继续之前等待 curl 完成?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44537939/

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