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 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分