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.
public static $userId; self::$userId =
.... and cal GetMesage::userId
.strpos('help', $yourString)
, no need for constants here.OutputMessage(GetMessage::$userId, GetMessage::$userText);
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');