xml_data to pass PHP variables

204 views Asked by At

I have integrated SMS Gateway in my Opencart CMS

It uses the following SYNTAX

    $xml_data ='<?xml version="1.0"?><smslist><sms><user>username</user
<password>112131</password><message>Your order # {$order_id} has been successfully received. Thank you for placing an Order at mystore.com</message><mobiles>9898000000</mobiles><senderid>Sherif</senderid><cdmasenderid>00201009546310</cdmasenderid><accountusagetypeid>1</accountusagetypeid></sms></smslist>';  

$URL = "http://mainadmin.dove-sms.com/sendsms.jsp?"; 

            $ch = curl_init($URL);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_ENCODING, 'UTF-8');
            curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/xml'));
            curl_setopt($ch, CURLOPT_POSTFIELDS, "$xml_data");
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            $output = curl_exec($ch);
            curl_close($ch);

Please make note that I have not used my personal credentials and this is just a syntax. I have tested the code and it works absolutely fine using my credentials

Now, What I want is it should load the order number, instead it shows the text {$order_id} in my message!

Moreover I want the text to be sent to the user who has logged-in in my stored by fetching the $telephone variable

Please help and thanks in advance

File in which this code is included: catalog/controller/checkout/sucess.php

Opencart version: 1.5.6

Find the Screenshot of the message I have Received here

1

There are 1 answers

13
Abdelrhman Adel On BEST ANSWER

Now, What I want is it should load the order number, instead it shows the text {$order_id} in my message!

That's because you are using a single quoted string for the variable $xml_data, single quoted strings in PHP display things as is (variables won't be substituted with their values, they will be treated as text), to solve this issue, use a double quoted string instead (according to the PHP manual)

Moreover I want the text to be sent to the user who has logged-in in my stored by fetching the $telephone variable

simply $telephone = $this->customer->getTelephone(), now you just need to put the variable $telephone in the correct tag in $xml_data

Edit

I have read the post comments and noticed that you get an error that says $order_id is not defined, to solve that, use $this->session->data['order_id'] instead of $order_id and place your SMS code in the beginning of the
if (isset($this->session->data['order_id'])) code block like that (to use the value of order id before it's erased from session):

if (isset($this->session->data['order_id'])) {
    $xml_data ="<?xml version="1.0"?><smslist><sms><user>username</user
<password>112131</password><message>Your order # {$this->session->data['order_id']} has been successfully received. Thank you for placing an Order at mystore.com</message><mobiles>9898000000</mobiles><senderid>Sherif</senderid><cdmasenderid>00201009546310</cdmasenderid><accountusagetypeid>1</accountusagetypeid></sms></smslist>";  
    $URL = "http://mainadmin.dove-sms.com/sendsms.jsp?"; 
    $ch = curl_init($URL);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_ENCODING, 'UTF-8');
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/xml'));
    curl_setopt($ch, CURLOPT_POSTFIELDS, "$xml_data");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($ch);
    curl_close($ch);

    // here put the rest of the code
    $this->cart->clear();
    // ...
}

Finally, delete cotenets of the the folder vqcache