简体   繁体   English

PHP curl_setopt:变量不能代替URL字符串

[英]PHP curl_setopt : Variable Not working in place of URL string

I use CURL in php, and I use CURL something like this 我在php中使用CURL,并且我使用CURL这样的东西

      $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 (); 这不起作用,但是如果我在curl_setopt()处写了“ http://exampledomain.com ”来代替“ $ smsURL”; 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 谢谢

You compose the URL from pieces but you don't encode the values properly. 您是从片段中组合URL,但没有正确编码值。 There are characters that have special meaning in URLs ( / , ? , & , = , % , URL中有些字符具有特殊含义( /?&=% , + 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. PHP通过函数urlencode()帮助您实现此目标,该函数可在创建查询字符串时用于分别编码每个值。 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() . 使用变量的名称作为键,将所需的所有值放入数组,然后将数组传递给函数http_build_query() There is no need to call urlencode() any more; 无需再调用urlencode()了; http_build_query() takes care of it. http_build_query()会处理它。 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. 检查HTTP状态代码列表以获取更多详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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