Shivam Pandya
Shivam Pandya

Reputation: 1061

PHP curl_setopt : Variable Not working in place of URL string

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

Upvotes: 1

Views: 1945

Answers (1)

axiac
axiac

Reputation: 72206

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.

Upvotes: 1

Related Questions