Click here to Skip to main content
15,921,716 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
hello im trying to convert my php code to JS since the company i work for needs my code to be in JS , to be honest im no completly sure how to do so
below is the PHP code who i need to be in JS:

PHP
<?php

function SendSMS($message_text,$recepients) {
    $sms_user = "USERNAME"; // User Name (Provided by Inforu)
    $sms_password = "PASSWORD"; // Password (Provided by Inforu)
    $sms_sender = "MyCompany"; //
    $message_text = preg_replace( "/\r|\n/", "", $message_text); // remove line breaks
    $xml = '';
    $xml .= '<Inforu>'.PHP_EOL;
    $xml .= ' <User>'.PHP_EOL;
    $xml .= ' <Username>'.htmlspecialchars($sms_user).'</Username>'.PHP_EOL;
    $xml .= ' <Password>'.htmlspecialchars($sms_password).'</Password>'.PHP_EOL;
    $xml .= ' </User>'.PHP_EOL;
    $xml .= ' <Content Type="sms">'.PHP_EOL;
    $xml .= ' <Message>'.htmlspecialchars($message_text).'</Message>'.PHP_EOL;
    $xml .= ' </Content>'.PHP_EOL;
    $xml .= ' <Recipients>'.PHP_EOL;
    $xml .= ' <PhoneNumber>'.htmlspecialchars($recepients).'</PhoneNumber>'.PHP_EOL;
    $xml .= ' </Recipients>'.PHP_EOL;
    $xml .= ' <Settings>'.PHP_EOL;
    $xml .= ' <Sender>'.htmlspecialchars($sms_sender).'</Sender>'.PHP_EOL;
    $xml .= ' </Settings>'.PHP_EOL;
    $xml .= '</Inforu>';
    $ch = curl_init();
    curl_setopt($ch,CURLOPT_URL,'http://uapi.inforu.co.il/SendMessageXml.ashx');
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, 'InforuXML='.urlencode($xml) );
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
    $response = curl_exec($ch);
    curl_close($ch);
    return $response;
    }


    ?>


What I have tried:

I did try to do it as the code in not working so i guess im not doing it right below is the code i converted to JS

function SendSMS(message_text, recepients)
{
    sms_user = "USERNAME";
    sms_password = "PASSWORD";
    sms_sender = "MyCompany";
    //
    message_text = Settlement.preg_replace("/\r|\n/", "", message_text);
    // remove line breaks
    xml = '';
    xml = '<Inforu>'+"\n";
    xml = ' <User>'+"\n";
    xml = ' <Username>'+Settlement.htmlspecialchars(sms_user)+'</Username>'+"\n";
    xml = ' <Password>'+Settlement.htmlspecialchars(sms_password)+'</Password>'+"\n";
    xml = ' </User>'+"\n";
    xml = ' <Content Type="sms">'+"\n";
    xml = ' <Message>'+Settlement.htmlspecialchars(message_text)+'</Message>'+"\n";
    xml = ' </Content>'+"\n";
    xml = ' <Recipients>'+"\n";
    xml = ' <PhoneNumber>'+Settlement.htmlspecialchars(recepients)+'</PhoneNumber>'+"\n";
    xml = ' </Recipients>'+"\n";
    xml = ' <Settings>'+"\n";
    xml = ' <Sender>'+Settlement.htmlspecialchars(sms_sender)+'</Sender>'+"\n";
    xml = ' </Settings>'+"\n";
    xml = '</Inforu>';
    ch = curl_init();
    curl_setopt(ch, CURLOPT_URL, 'http://uapi.inforu.co.il/SendMessageXml.ashx');
    curl_setopt(ch, CURLOPT_POST, true);
    curl_setopt(ch, CURLOPT_POSTFIELDS, 'InforuXML='+Settlement.urlencode(xml));
    curl_setopt(ch, CURLOPT_RETURNTRANSFER, 1);
    response = curl_exec(ch);
    curl_close(ch);
    return response;
}
Posted
Updated 19-Oct-22 3:10am
Comments
Richard Deeming 19-Oct-22 8:18am    
1) This is NOT a code-conversion service.

2) Unless the uapi.inforu.co.il site is explicitly configured to allow access from your site (Cross-Origin Resource Sharing (CORS) - HTTP | MDN[^]), you won't be able to send requests to it from Javascript.

1 solution

You do realize that the two languages run on different computers, right?
PHP runs on your server, and uses server resources.
Javascript runs on the client, inside a browser sandbox which denies it access to local (client) machine resources.
it is very unlikely that the JS can send the SMS directly, and even if it could, any required access passwords would be exposed to every user who can press F12 ...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900