简体   繁体   中英

How to delay a curl request in PHP if my content in foreach loop?

I am using a php script, where I want to send a sms through the api with cURL, but if my phone no is multiple (it is in an array) and I used the foreach to send the sms for every contact no, but when I use cURL request in foreach then it is taking only first contact no, and after that it is not able to send the sms to another no which is available into my contact array.

Here is the code to understand.

function sendSMS($data){
    $phones = $data['recipient_contact'];
    $explode_contact = explode(",", $phones);
    //read all the contact and send the mail
    foreach($explode_contact as $k=>$v){
        $contact_number = '91'.$v;
        $sms_body = $data['sms_body'];
        $curl  = curl_init();
        $data = array(
            'aid'=>'XXXX',
            'pin'=>'XXXX',
            'mnumber'=>$contact_number,
            'message'=>$sms_body,
            'singnature'=>'XXXXX');
        $url = sprintf("%s%s","http://mysmslink/HttpLink",http_build_query($data));
        curl_setopt($curl,  CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $result = curl_exec($curl);
        sleep(5);
        curl_close($curl);
    }
    return "SMS Send Successfully";
}

in you php.ini set execution timeout in second as your need

max_execution_time = 60

if you want to define it in php file. Add this line

ini_set('max_execution_time', 60);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM