want to send and receive json data in php

前端 未结 3 2033
滥情空心
滥情空心 2021-02-10 23:34

As per application requirement, I am trying to develop two PHP which can communicate with each other via Json. I tried searching online but didn\'t found solution.

Can

3条回答
  •  面向向阳花
    2021-02-11 00:16

    Remember that JSON means 'object notation', ie it's a way to describe an object in javascript. It's a great way to communicate at the network-level, but when you're in PHP you should be using the data structures that PHP is designed to use. Rather than work with JSON directly on either side, and especially as an alternative to a big JSON string, keep that data structured as an array and encode it right before sending. Your approach with curl is fine, though a bit custom - there's lots of nice routers that do a better job managing these sorts of requests (symfony is my favourite), but that's a separate issue.

    e.g. instead of your big string, represent it as:

    $data = 
        [ "user" => [   
                     [ "firstName" => "Vignesh",  
                       "lastName"  => "Prajapati",
                       "age"       => 23,
                       "email"     => ["vignesh@gmail.com","vignesh@yahoo.com"],
                       "subject"   => ["English","Gujarati", "Hindi"]
                     ]
    

    etc.. When it comes time to send it to the other server, json_encode and go.

提交回复
热议问题