是否等待 curl 完成后再继续?

wtzytmuj  于 2022-11-13  发布在  其他
关注(0)|答案(2)|浏览(144)

我正在使用一个脚本通过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
sc4hvdpw

sc4hvdpw1#

你应该检查一下 curl React

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
    }
}
zrfyljdw

zrfyljdw2#

尝试将传输参数返回为true以等待响应:

// Some more curl code here

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

    // 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

相关问题