1

I use CURL in php, and I use CURL something like this

      $url = "http://exampledomain.com";
      $smsURL = $url;

      $curl = curl_init();
      curl_setopt ($curl, CURLOPT_URL, $smsURL);
      curl_exec ($curl);
      curl_close ($curl);

This is not working, but if I wrote "http://exampledomain.com" in place of "$smsURL" at curl_setopt (); It will work fine. Where is issue in my code? did I miss something?

Original Code

          $url = $this->conf['sms_getway_url'];
      $url .= '&recipient=' . $_POST['txt_customer_contact_no'];
      $url .= '&sender=' . strtoupper($saloon_info['saloon_name']);
      $url .= '&is_payor=' . $this->conf['sms_is_payor'];
      $url .= '&pay_amount=' . $this->conf['sms_pay_amount'];
      $url .= '&token=5ce7467e9ec045cbbac448ba5a422a02';
      //$url .= '&customer_num=' . $this->conf['sms_customer_num'] . $saloon_id;
      $url .= '&customer_num=' . $this->conf['sms_customer_num'];
      $appointment_time = date('H:i', strtotime($app_start_time));
      $employee_name = $_POST['hdn_selected_employee_name']; //$value['id_employee'];
      //$sms_msg = "Hey. Recalling that I await tomorrow at. " . $appointment_time . " Regards " . $employee_name . ", " . $saloon_name . ". ";
      $sms_msg = t('msg_sms_book_appointment', array('%emp_name' => $employee_name, '%saloon_name' => $_POST['hdn_selected_saloon_name'], '%time' => $appointment_time));
      $url .= '&sms_msg=' . $sms_msg;

        $smsURL = $url;

        $curl = curl_init();
        curl_setopt ($curl, CURLOPT_URL, $smsURL);
        curl_exec ($curl);
        curl_close ($curl);

Thanks

4
  • Example is fine, now show us some real code. Commented Feb 12, 2015 at 12:31
  • What exactly is not working? Does the call to curl_seopt() show any error? Or curl_exec() does not return what you expect? Commented Feb 12, 2015 at 12:36
  • @axiac I enter data in DB via CURL, If I pass string then its work but when I pass data via variable, then its not working Commented Feb 12, 2015 at 12:37
  • 1
    It depends of the string you pass. If your $smsURL does not contain the correct URL, of course it does not work. Try to echo($smsURL); then put the exact output of echo() as string in the call to curl_setopt() instead of $smsURL. I bet it will fail. Check the value returned by curl_exec() and use curl_getinfo($curl, CURLINFO_HTTP_CODE) after curl_exec() to find the returned response code. If it's greater than 400 then your request failed. And it does because the values in your URL are not properly encoded. Commented Feb 12, 2015 at 12:44

1 Answer 1

1

You compose the URL from pieces but you don't encode the values properly. There are characters that have special meaning in URLs (/, ?, &, =, %, , + and a few more). They have to be encoded when they appear in the values from the query string, in order to retain their literal meaning.

PHP helps you for this goal with function urlencode() that can be used to encode each value individually when you create a query string. Something like this:

$url  = $this->conf['sms_getway_url'];

$url .= '&recipient=' . urlencode($_POST['txt_customer_contact_no']);
$url .= '&sender=' . urlencode(strtoupper($saloon_info['saloon_name']));
...

But, because this is a tedious work, it also provides an easier method. Put all the values you need into an array, using the names of the variables as keys, then pass the array to function http_build_query(). There is no need to call urlencode() any more; http_build_query() takes care of it. Also it puts ampersands (&) between the variables and equals (=) where they belong.

The code is like this:

$url = $this->conf['sms_getway_url'];

// Prepare the values to put into the query string
$vars = array();
$vars['recipient']    = $_POST['txt_customer_contact_no'];
$vars['sender']       = strtoupper($saloon_info['saloon_name']);
$vars['is_payor']     = $this->conf['sms_is_payor'];
$vars['pay_amount']   = $this->conf['sms_pay_amount'];
$vars['token']        = '5ce7467e9ec045cbbac448ba5a422a02';
$vars['customer_num'] = $this->conf['sms_customer_num'];

$appointment_time = date('H:i', strtotime($app_start_time));
$employee_name    = $_POST['hdn_selected_employee_name'];
$sms_msg = t('msg_sms_book_appointment', array(
    '%emp_name'    => $employee_name,
    '%saloon_name' => $_POST['hdn_selected_saloon_name'],
    '%time'        => $appointment_time,
));
$vars['sms_msg']  = $sms_msg;


// Now, the magic comes into place
$smsURL = $url.'?'.http_build_query($vars);

$curl = curl_init();
curl_setopt ($curl, CURLOPT_URL, $smsURL);
if (! curl_exec ($curl)) {
    // Something went wrong. Check the status code (at least)
    $code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
    // Do something here.
    // If $code >= 500 then the remote server encountered an internal error
    //                      retry later or ask them to fix it
    // If 400 <= $code < 500 then there is a problem with the request:
    //                            maybe the resource is not there (404, 410)  
    //                         or you are not allowed to access it (403)
    //                         or something else.
    echo('Failure sending the SMS. HTTP status code is '.$code."\n");
}
curl_close ($curl);

Check the list of HTTP status codes for more details.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.