S
S
Sergey08082017-06-21 20:57:56
PHP
Sergey0808, 2017-06-21 20:57:56

Php Phalcon how to send data in json format?

Good evening, there is such a task:
There are two sites on localhost. It is necessary to request and receive data from the second site (the first one requests). The controller action code of the first site that requests:

public function acceptAction()
        {

            $json = file_get_contents("php://input");
            $receive_data = json_decode($json,true);
            $users = $receive_data;

            $this->view->users = $users;
       }

The action code of the controller of the second site, which gives the data:
public function sendAction()
        {

            $users = Users::find();
            $send_data = json_encode($users);

            $curl = curl_init();
            curl_setopt($curl,CURLOPT_URL,"http://learning/test/accept");
            curl_setopt($curl, CURLOPT_HTTPHEADER, array(
                'Accept: application/json',
                'Content-type: application/json')
            );
            curl_setopt($curl,CURLOPT_POST,true);
            curl_setopt($curl,CURLOPT_POSTFIELDS,$send_data);
            curl_setopt($curl,CURLOPT_RETURNTRANSFER,true);
            curl_setopt($curl,CURLOPT_HEADER,false);
            curl_exec($curl);

            curl_close($curl);
        }

Why is the data not coming, please help.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Pavel Volintsev, 2017-06-22
@Sergey0808

0. Make sure in /etc/hosts that the domain learningbelongs to the host 127.0.0.1
1. Check manually via the console command curl on the same server that http://learning/test/acceptreturns any response
2. Check the web server
access.log logs - was there general query between two scripts
error.log - were there any errors
3. In the code sendAction(), replace the code to see the error

if( ! $result = curl_exec($curl)) // чтобы проверить на наличие ошибки CURL или HTTP
{
    trigger_error(curl_error($curl));
}
curl_close($curl);
echo $result; // чтобы видеть результат запроса

4. In Code acceptAction(), Add Code
$receive_data = json_decode($json,true);
if (json_last_error()) // чтобы проверить наличие ошибки декодирования JSON
{
    trigger_error(json_last_error_msg());
}

-------------
In general, what is the logic of your script?
I see that
1. The first script selects users from the database (no limit, it's scary in production)
2. This list of users is sent as a request from public function sendAction()to http://learning/test/acceptin JSON format
3. This JSON is decoded and passed to $this->view->users
4. ... why?
-----------
Refined solution
Get JSON on one server and show via template
class TestController
{
    public function showAction()
    {
        $json = file_get_contents("http://learning/test/users"); // получить данные со второго сервера
        $users = json_decode($json, true);
        $this->view->users = $users; // передать в представление
    }
}

On the second server, select data from the database and issue it in JSON format
class TestController
{
    public function usersAction()
    {
        $users = Users::find(); // получить из базы

        // подготовиться к выдаче ответа в формате JSON
        $this->view->disable();
        $response = new \Phalcon\Http\Response();
        $response->setStatusCode(200);
        $response->setJsonContent($users);
        $response->setContentType('application/json', 'UTF-8');
        return $response;
    }
}

In theory, the first one should show what is stored in the database in the user table on the second server

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question