0

I work as a web designer at a small startup and I've been tasked with creating a PHP program/file that will receive JSON data from our app's backend server whenever a new user signs up for the app or does a number of other events, process the data and send it to our CRM (Insightly).

I have a fairly good understanding of PHP when it comes to designing websites, but I've never really used it to process data like this. My original idea was to set up a .json file on our Azure server for each event and have the backend write the data to one of these files. The data is coming as POST data (not from a form) and I was using php://input to collect the data and write it to the .json files. The Azure server would then periodically run my PHP program to check if there's data in the file, and send the data to Insightly if there is.

However, the developer I'm working with wants to receive a new Contact ID/Project ID/etc that Insightly generates as an HTTP response immediately. So basically:

  1. backend server sends json data either to .json file or PHP script on server
  2. PHP script either immediately reads .json file or just json_decodes the data into an array to send to Insightly
  3. Insightly creates a new project/contact/organization and returns ID that it creates to PHP script
  4. PHP script sends back that ID as an HTTP response to the backend

So my questions are how can I have PHP "listen" to the .json files to immediately run the Insightly operations and/or how can I send back an HTTP response to the backend server once the Insightly update is complete? Really, what's the best way to be going about this? Any advice or suggestions help.

Peter Pan
  • 23,476
  • 4
  • 25
  • 43
  • Welcome to Stack Overflow! You seem to be asking for someone to write some code for you. Please provide some code how you tried to solve this problem. Do your research first, and try to ask question that is more specific. – nakashu Feb 16 '16 at 21:01

2 Answers2

2

Without getting into too many specifics, you don't want to use a file to receive the JSON. Have the server point to one of your PHP scripts (ie JSONhandler.php). You can then decode the JSON into an object, do whatever you need to do with it, then echo a response. This is a basic example of how REST apis work.

$jsonstr = $_POST['data-in'];
$jsonObj = json_decode($jsonstr);

//do something with the data

echo $id; //where id is generated by your insight code

OffTheBricks
  • 425
  • 2
  • 9
1

Every PHP script on your Azure Service, can be treated as a collection of several REST APis.

Assume you use Azure Web Apps to host your PHP scripts. You can create one PHP script to handle all these operations.

We imitate the PHP script named "handler.php", then there is an endpoint of this script like "http://<you_azure_web_app_name>.azurewebsites.net/handler.php". Your backend will send data to this url directly.

Then you can create the PHP script like this:

<?php
    //get post data
    $post_data = file_get_contents('php://input');
    //convert data from jsonstr to (array) stdclass object 
    $data = (array) json_decode($post_data);

    //handle $data 
    $handled_data = some_handle_operations($data);

    //post your handled data to Insightly and get the id
    $response_result = some_post_functions_depends_your_choice($handled_data);

    //handle the respone result to get the id (what you want here)
    $id = some_operations_to_get_id_from_result($response_result);

    //response to your backend
    echo $id;

And to make HTTP request in PHP, there are several popular functions.

Community
  • 1
  • 1
Gary Liu
  • 13,758
  • 1
  • 17
  • 32