Sebastian Kopiczko Sebastian Kopiczko - 3 months ago 8
JSON Question

Getting JSON input and pass it into a function in PHP

I've got 4 classes responsive for:

1st - getting input as JSON string from url, decode it into an array and create 2

variables: $user_id, $user_text (like here below):


class Get_message{
public $user_id, $user_text;

public function get_input(){

$input = json_decode(file_get_contents('myURL'), true);
$user_id = $input['getting value from an array'];
$user_text = $input['getting value from an array'];

//how to return it?
//return $input, $user_id, $user_text? it won't work I guess
}
}


and there is my first question: how to return that 2 values so that I can use it in another classes?

2nd - check if that string contains keywords, if it contains a keyword I want to create another JSON,

so far I've got this:

class input_recognize{
const KEYWORD_HELP = 'help';
//...some more constants...
const KEYWORD_REPORT = 'report';

public function msg_recognize($user_text)
{
switch ($user_text) {
case self::KEYWORD_HELP:
return new Output_msg(Output_msg::MESS_HELP);
break;
//... some more cases
case self::KEYWORD_REPORT:
return new Output_message(Output_msg::MESS_REPORT);
break;
}
}


}

3rd - based on that what case in class above was true create appropriate JSON, my code looks like:

class Output_message{

const MESS_REPORT = 1;
const MESS_HELP = 2;

public function __construct($user_id, $user_text){
//I guess I should use $this here, dunno how

$json_output = array(
"first_title" => array(
"id" => $user_id
),
"second_title" => array(
"text" => "text to send"
),
);
$output = json_encode($json_ouput);
}
}


4th - and I want to be able to take $output into that function and POST it to url.

I'm beginner in php, object oriented programming as well, I'll appreciate any help, and criticism. Thanks in advice.

Answer
  1. You can use the same class to do everything you have done, using methods. For those input variables declare them as public static and use your class constructor to set them. public static $userId; self::$userId = .... and cal GetMesage::userId.
  2. Use strpos('help', $yourString), no need for constants here.
  3. I don't think you need to do that in a class constructor, if you do you can add more methods. Like you set you values in constructor new OutputMessage(GetMessage::$userId, GetMessage::$userText);
  4. use a method like sendOutput() and post your data to url using curl.

I do not advice you to do it like you did, you can use only a class, and pass data form a method to another, you won't really need output or input, only the 2 url's. If you really want 4 classes then you can chain them, one class will return another.

This is just demonstrative, there are a loot of ways of achieving what you want, this is an example:

class Message{
    public static $userId;
    public static $userText;
    public static $messageType;

    public function __construct($inputUrl)
    {
        $this->getMessage($inputUrl);
        $this->parseMessage();
    }

    private function getMessage($inputUrl){

        $input = json_decode(file_get_contents($inputUrl), true);
        self::$userId = $input['id'];
        self::$userText = $input['text'];
    }

    private function parseMessage()
    {
        $messageText = self::$userText;
        switch (true) {
            case (strpos('help',$messageText) !== false):
                self::$messageType = "Help"; //or code, or whatever you want here
                break;
            //... some more cases
            case (strpos('report',$messageText) !== false):
                self::$messageType = "Report"; //or code, or whatever you want here
                break;
        }
    }

    public function sendResponse($outputUrl){

        $jsonOut = array(
            "first_title" => array(
                "id" => self::$userId
            ),
            "second_title" => array(
                "text" => "text to send"
            ),
        );
        $output = json_encode($jsonOut);

        $ch = curl_init($outputUrl);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_POSTFIELDS, $output);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                'Content-Type: application/json',
                'Content-Length: ' . strlen($output))
        );
        return curl_exec($ch);
    }
}

$sendMessage = new Message('inputUrl');
$sendMessage->sendResponse('outputUrl');
Comments