how to convert destinationAddresses/phone number into hash codes in PHP sms API

I want to create SMS sending application using&nbsp;<strong>PHP</strong>&nbsp;with&nbsp;<strong>SMS API</strong>. In my response message I can see success code and success message but my Problem is in my log file says as

I want to create SMS sending application using PHP with SMS API. In my response message I can see success code and success message but my Problem is in my log file says as

Passed Exception exception 'SMSServiceException' with message 'Format of the address is invalid'

I think problem is, starts when I hash coding phone number. And I don't know how to do that, Please help me.

I have tried to convert phone number using md5() but result is same.

my index.php

$jsonData = array( 'requestId'=> '','message' => 'thismsg hello',
  'password' => '25c8db49905003e3347ad861546fce1a',
  'sourceAddress' => '77000',
  'deliveryStatusRequest' => '1',
  'chargingAmount' => '2.00',
  'destinationAddresses' => ['88b7a1e8dbf419a2c0835b4f33d06c1a'],//this convert with md5
  'applicationId' => 'APP_051000',
  'encoding' => '0',
  'version' => '1.0',
  'binaryHeader' => ''
);

my .log file result:

[01-Feb-2019 08:57:34 Asia/Colombo] Message received msg_header hello
[01-Feb-2019 08:57:35 Asia/Colombo] Passed Exception exception 'SMSServiceException' with message 'Format of the address is invalid.' in /ophielapp/lib/SMSSender.php:58
Stack trace:
#0 /ophielapp/lib/SMSSender.php(46): SMSSender->handleResponse(Object(stdClass))
#1 /ophielapp/lib/SMSSender.php(34): SMSSender->sendRequest('{"applicationId...')
#2 /ophielapp/sms.php(66): SMSSender->sendMessage('hello', '77000')
#3 {main}

I want to correctly send phone number into smsSender.php with hash code and If want to more details of other php file I can provide it.

result in my browser:this is the image of response

this is my smsSender.php

<?php

require_once 'SMSServiceException.php';
class SMSSender{
private $applicationId,
$password,
$serverURL;

public function __construct($serverURL, $applicationId, $password)
{
        $this-&gt;applicationId = $applicationId;
        $this-&gt;password = $password;
        $this-&gt;serverURL = $serverURL;
}

public function broadcastMessage($message){
    return $this-&gt;sendMessage($message, array('tel:all'));
}

public function sendMessage($message, $addresses){
    if(empty($addresses))
        throw new SMSServiceException('Format of the address is invalid.', 'E1325');
    else {
        $jsonStream = (is_string($addresses))?$this-&gt;resolveJsonStream($message, array($addresses)):(is_array($addresses)?$this-&gt;resolveJsonStream($message, $addresses):null);
        return ($jsonStream!=null)?$this-&gt;sendRequest($jsonStream):false;

    }
}

private function sendRequest($jsonStream){
    $opts = array('http'=&gt;array('method'=&gt;'POST',
                                'header'=&gt;'Content-type: application/json',
                                'content'=&gt;$jsonStream));
    $context = stream_context_create($opts);
    $response = file_get_contents($this-&gt;serverURL, 0, $context);

    return $this-&gt;handleResponse(json_decode($response));
}

private function handleResponse($jsonResponse){
    $statusCode = $jsonResponse-&gt;statusCode;
    $statusDetail = $jsonResponse-&gt;statusDetail;

    if(empty($jsonResponse))
        throw new SMSServiceException('Invalid server URL', '500');
    else if(strcmp($statusCode, 'S1000')==0)
        return true;
    else
        throw new SMSServiceException($statusDetail, $statusCode);
}

private function resolveJsonStream($message, $addresses){
    // $addresses is a array

    $messageDetails = array('message'=&gt;$message,
                            'destinationAddresses'=&gt;$addresses);

    $applicationDetails = array('applicationId'=&gt;$this-&gt;applicationId,
                                'password'=&gt;$this-&gt;password);

    $jsonStream = json_encode($applicationDetails+$messageDetails);

    return $jsonStream;
}

public function get_location_stream( $addresse){

            $reqDetails = array(
                                    'applicationId'=&gt;$this-&gt;applicationId,
                                    'password'=&gt;$this-&gt;password,
                                    'serviceType'=&gt;'IMMEDIATE',
                                    'subscriberId'=&gt;$addresse
                                );

            return json_encode($reqDetails);

}

public function getlocation( $addresse){

    //$jsonStream = get_location_stream($addresse);

    $jsonStream= array(
                                    'applicationId'=&gt;$this-&gt;applicationId,
                                    'password'=&gt;$this-&gt;password,
                                    'serviceType'=&gt;'IMMEDIATE',
                                    'subscriberId'=&gt;$addresse
                );
    print_r($jsonStream);
    json_encode($jsonStream);                           
    $opts = array('http'=&gt;array('method'=&gt;'POST',
                                'header'=&gt;'Content-Type: application/json',
                                'content'=&gt;$jsonStream));

    $context = stream_context_create($opts);
    $response = file_get_contents('http://localhost:7000/lbs/locate', 0, $context);

    echo $response;

    //return $this-&gt;location_response(json_decode($response));
    return json_decode($response);
}

public function location_response($jsonResponse){
    $statusCode = $jsonResponse-&gt;statusCode;

    if(empty($jsonResponse)){

        throw new SMSServiceException('Invalid server URL', '500');

    }else if(strcmp($statusCode, 'S1000')==0){

         return array(
                        $jsonResponse-&gt;longitude, 
                        $jsonResponse-&gt;latitude,
                        $jsonResponse-&gt;horizontalAccuracy,
                        $jsonResponse-&gt;freshness,
                        $jsonResponse-&gt;messageId

                    );

    }else{

        throw new SMSServiceException($statusDetail, $statusCode);
    }
}

  public function getResponse($addresse){

    $jsonStream= array(
                                    'applicationId'=&gt;$this-&gt;applicationId,
                                    'password'=&gt;$this-&gt;password,
                                    'serviceType'=&gt;'IMMEDIATE',
                                    'subscriberId'=&gt;$addresse
                        );


    $opts = array('http'=&gt;array('method'=&gt;'POST',
                                'header'=&gt;'Content-Type: application/json',
                                'content'=&gt;json_encode($jsonStream)));

    $context = stream_context_create($opts);
    $response = file_get_contents('http://localhost:7000/lbs/locate', 0, $context);

    return json_decode($response);
}

}
?>


PHP Interview Questions with Solutions: Prepare for PHP Interview

PHP Interview Questions with Solutions: Prepare for PHP Interview

Prepare for PHP Interviews. Set Variable With php.ini File. Logic & Output Behind PHP Code Segment. Class Concept, Error & Functions in PHP. Start PHP Now!

Description
In this course you will be introduced with some tricky questions that everyone face during their interview. In each solutions I have included some useful functions which we generally use at the time of development also. I have covered the following area in my course with Questions, Attractive Presentations and Practical Solutions that will help you to understand the logic behind PHP in a different way.

Some useful functions
PHP error types
Class concept
Access specifiers public, private and protected
Set variable with php.ini file
Operators introduced in PHP7
Logic and its output behind some code segment
Basic knowledge
PHP7, XAMPP Server, Notepad++
What will you learn
This course is build for the person who is facing interviews. Every question is well explained with the practical solution through videos. So that everybody can prepare themselves for the tricky questions asked during interviews
To continue:

Advantages of Hiring PHP Developer for your Website Project

Advantages of Hiring PHP Developer for your Website Project

PHP - Hypertext pre-processor, a scripting language used by many people in developing web pages, but most of us are unaware even of the full form. To train someone and make them learn this whole language is as difficult and time-consuming as it is...

PHP - Hypertext pre-processor, a scripting language used by many people in developing web pages, but most of us are unaware even of the full form. To train someone and make them learn this whole language is as difficult and time-consuming as it is to learn the language yourself. That’s why PHP developers are there to make your life easy. This article will give us the advantages and requirements of Hire PHP Developer for our very own website project.

First of all, let us understand the value the right developer brings to the project and why it is important for your business.

A website is a major component of any company/business and is very important for its face value, the way it represents the company on the internet is critical for any business to succeed. This is the reason why companies are looking for PHP developers who can develop their webpage.

If you're planning to do an online business, your PHP programmer will be the first person to transfer your thinking onto the webpage. You should, therefore, employ developers from PHP to make your hypothetical idea a reality.

With this software programming language, PHP developers all-around can easily build website frameworks, web content management systems, web template systems, and various other web-based designs.

Some of the reasons why we need to outsource these developers are:

Not everyone is the best in each field, all of us have our specific skills and talents hence, PHP developers are also the best at what they do. The time and money spent on the training of the in house employees would be saved if the professional PHP developers are hired. Instead of multitasking, if the employees were to focus on what they’re good at it would increase productivity too.

The PHP developers would be much more professional than the in-house workers. It would lead to the seriousness of work. Hence, on-time delivery is guaranteed with hired PHP developers.

In addition to these benefits, you would also be able to track your project through every stage in constant communication with your online team. These advantages make it incredibly popular and smart to hire a PHP developer.

The PHP developers have in-depth knowledge of PHP, HTML and various frameworks in terms of technical capabilities. Hiring PHP developers are advised to give your website a professional look based on PHP.

Much of web success depends on the involvement of social media. The developer can add to your social networking pages a feature that explicitly redirects visitors. In addition, SEO experts also suggest better connections to the website's social network.

Just like a tailor stitches our dresses according to our preferences and is ready to make last-minute changes. A PHP developer will also be available at the nick of your call to make the website just the way you want it to be and have a customized solution for every problem.

Read also: Why & How to Hire Dedicated PHP Developer

At some point in your business, you’re going to have problems regarding your webpage due to the rapidly changing technology, instead of struggling with ideas like these and not being able to come up with an appropriate solution a PHP web developer could help us with our problems just like any technician would help us with the problems we face in our offices or any architect would help us with designing the structure of a building or any interior designer would help us with setting up our home. The PHP development company are hubs of workers who would help us overcome these problems and are always there.

Source by

Using JSON correctly in JavaScript and PHP

Using JSON correctly in JavaScript and PHP

JSON is an indispensable part for web applications. Here you will learn how to exchange and store JSON data correctly between JavaScript (client-side) and PHP (server-side).

Table of contents
  • Reading and saving JSON in PHP
  • Read JSON
  • Save JSON
  • Send JSON from PHP to JavaScript
  • JSON in JavaScript
  • Conclusion

JSON stands for JavaScript Object Notation and is a very simple and compact data format for storing and sending data. Data is often exchanged between the user (client) and the server. In this article we look at the data transfer and storage of JSON.

{
  "ceos": [
    {
      "id": "1",
      "name": "Steve Jobs"
    },
    {
      "id": "2",
      "name": "Bill Gates"
    },
    {
      "id": "3",
      "name": "Paul Allen"
    }
  ]
}

This is an example for JSON. Three entries with the attributes id and name are stored in the category ceos.

You can validate and format your JSON with this tool.

Since I would like to go more into the use of JavaScript and PHP here, you can have a look at the exact structure of JSON in this article if you are interested.

Reading and saving JSON in PHP

Reading JSON

In principle, JSON data can be stored very easily. A simple text file is sufficient to store the data there. When it comes to small amounts of data, this is also a common and good solution. However, if you have a lot of data, or data that is added dynamically (like a contact form), it is recommended to store the data in a database.

But here we start from small amounts of data and on the server side we have a file named storage.json with the following content (same content as the example above, minified only):

{"heroes":[{"id":"1","name":"Steve Jobs"},{"id":"2","name":"Bill Gates"},{"id":"3","name":"Paul Allen"},{"id":"4","name":"Sundar Pichai"}]}

The following lines can be used to read in and output the file:

<?php 
$file = file_get_contents("storage.json");
print_r($file);
?>

The unformatted output gives us the simple text content of the file:

{"heroes":[{"id":"1","name":"Steve Jobs"},{"id":"2","name":"Bill Gates"},{"id":"3","name":"Paul Allen"},{"id":"4","name":"Sundar Pichai"}]}

We can’t do much with this data in this form. We could write our own parser to convert it into an object or array. But it is much easier.

PHP offers us the function json_decode() to convert the JSON string into an object.

<?php 
$file = file_get_contents("storage.json");
$json_decoded = json_decode($file);
print_r($json_decoded);
?>

The JSON string has been converted to an object and we can treat it like any other object in PHP. This output looks like this:

stdClass Object
(
    [heroes] => Array
        (
            [0] => stdClass Object
                (
                    [id] => 1
                    [name] => Steve Jobs
                )
        [1] =&gt; stdClass Object
            (
                [id] =&gt; 2
                [name] =&gt; Bill Gates
            )

        [2] =&gt; stdClass Object
            (
                [id] =&gt; 3
                [name] =&gt; Paul Allen
            )

        [3] =&gt; stdClass Object
            (
                [id] =&gt; 4
                [name] =&gt; Sundar Pichai
            )

    )

)

As already mentioned we can now use normal object operators to display or change the values of the object:

<?php
$file = file_get_contents("storage.json");
$json_decoded = json_decode($file);

echo $json_decoded->heroes[0]->name; // Output: Steve Jobs

$json_decoded->heroes[0]->name = "CEO Steve Jobs";

echo $json_decoded->heroes[0]->name; // Output: CEO Steve Jobs
?>

Save JSON

Once we have adjusted our data as desired, we can save it again.

<?php
$filename = "storage.json";
$file = file_get_contents($filename);
$json_decoded = json_decode($file);

$json_decoded->heroes[0]->name = "CEO Steve Jobs";

$json_encoded = json_encode($json_decoded);
file_put_contents($filename, $json_encoded);
?>

Since we decoded the JSON string while reading it out, we have to encode it again before saving it. This is done in PHP via json_encode(). That’s it, too. Simple, isn’t it?

Send JSON from PHP to JavaScript

To request data on the client side I like to use jQuery. Here is an example, which makes an Ajax request to our server.php and gets the data returned.

index.html

<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8" />
<title>JSON in JavaScript and PHP</title>
</head>

<body>

&lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"&gt;&lt;/script&gt;
&lt;script&gt;
    $.getJSON('server.php', {}, function(data) {
        console.log(data);
    });
&lt;/script&gt;

</body>

</html>

server.php

<?php
$file = file_get_contents("storage.json");
exit($file);
?>

The console output looks like this:


This gives us normal access in JavaScript to the data that originally came from our storage.json.

JSON in JavaScript

If we now have a JSON string in JavaScript, we can convert it to a JavaScript object using the JSON.parse() function. It looks like this:

var json = '{"heroes":[{"id":"1","name":"CEO Steve Jobs"},{"id":"2","name":"Bill Gates"},{"id":"3","name":"Paul Allen"},{"id":"4","name":"Sundar Pichai"}]}';

var obj = JSON.parse(json);
console.log(obj);

The output is identical to the output from our storage.json.


Conversely, we can convert a JavaScript object to JSON with JSON.stringify().

var json = '{"heroes":[{"id":"1","name":"CEO Steve Jobs"},{"id":"2","name":"Bill Gates"},{"id":"3","name":"Paul Allen"},{"id":"4","name":"Sundar Pichai"}]}';

var obj = JSON.parse(json);

var jsonAgain = JSON.stringify(obj);
console.log(jsonAgain);

The console output is then again our JSON string, which we also have in the variable json.

{"heroes":[{"id":"1","name":"CEO Steve Jobs"},{"id":"2","name":"Bill Gates"},{"id":"3","name":"Paul Allen"},{"id":"4","name":"Sundar Pichai"}]}
Conclusion

We have discussed the most important JSON functions for JavaScript and PHP. JSON is simply a very simple and compact data format. As you have now seen, the use in JavaScript and PHP is also quite simple. If you have any questions, please feel free to contact me via the comments! 

Originally published  at webdeasy.de on 21. May 2019

========================================

Thanks for reading :heart: If you liked this post, share it with all of your programming buddies! Follow me on Facebook | Twitter

☞ The complete beginner’s guide to JSON

☞ The Complete Guide to JSON Web Tokens

☞ Svelte.js - The Complete Guide

☞ The Complete JavaScript Course 2019: Build Real Projects!

☞ The Complete Node.js Developer Course (3rd Edition)

☞ PHP for Beginners - Become a PHP Master - CMS Project

☞ Learn Object Oriented PHP By Building a Complete Website

☞ MEVP Stack Vue JS 2 Course: MySQL + Express.js + Vue.js +PHP

☞ Object Oriented PHP & MVC

☞ PHP OOP: Object Oriented Programming for beginners + Project