douzhang8144 2016-08-09 09:35
浏览 90
已采纳

获取JSON输入并将其传递给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.

  • 写回答

1条回答 默认 最新

  • duankangzi766767 2016-08-09 10:05
    关注
    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');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)