在继续之前等待卷曲完成?

时间:2017-06-14 07:15:07

标签: php 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

2 个答案:

答案 0 :(得分:0)

您应该检查卷曲响应

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

答案 1 :(得分:0)

尝试将return transfer参数设置为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