I need some help in SMS API script for PHP.

Discussion in 'PHP' started by TechListen, Apr 14, 2011.

  1. #1
    Hello, I have written a PHP script which sends SMSes using FreeSMSAPI. But, I want to parse the JSON API response in PHP. I want some fields from JSON, like the mobile number, etc. By default, it shows the default message. I want to show my own message when message gets sent or fails.
    The API response in JSON comes like this:
    {
        "response":{
            "success":{
                "message":"Message Send Successfully",
                "number":["9699419699","9892098920"]
            }
        }
    }
    HTML:
    My script written in PHP:
    <?php
    $skey = "mysecretkey";
    $tonumber = $_POST['recnumber'];
    $tomessage = $_POST['message'];
    $response = file_get_contents("http://s2.freesmsapi.com/messages/send?skey=$skey&message=".urlencode($tomessage)."&senderid=MyID&recipient=$tonumber&format=json");
    $json_a = json_decode($response);
    
    if($json_a->status === "success")
    {
    echo $json_a->number;
    }
    else
    {
    echo 'Message not sent.';
    }
    ?>
    HTML:
    When I use the above PHP code, it doesn't show anything. It shows only blank page.
     
    TechListen, Apr 14, 2011 IP
  2. SametAras

    SametAras Well-Known Member

    Messages:
    53
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    113
    #2
    Finally, you had better use print_r($json_a); thus you can see array. After, you write output to here.
     
    SametAras, Apr 14, 2011 IP
  3. TechListen

    TechListen Peon

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    You mean, I should change "echo" to "print" ?
     
    TechListen, May 24, 2011 IP