0

I have an existing PHP script, which essentially connects to 2 databases each on a different server and performs a few MySQL queries on each. The ultimate results are stored in a data array which is used to write said results into a JSON file.

All of this works perfectly. The data is inserted into the mysql table correctly and the JSON file is exactly the way it should be.

However, I need to add a block to the end of my script that makes a POST request to one of our affiliate's API and upload the info there. We're currently manually uploading this JSON file to the api instance but we have the configuration data for their server to use in a POST request now so that when this script is run it automatically sends the data rather than us having to manually update it.

The main thing is I'm not exactly sure how to go about that. I've started with code for doing this but I'm not familiar with cURL so I don't know the best way to structure this in php.

Here is an example the affiliate gave me in cURL command line syntax:

curl \
-H "Authorization: Token AUTH_TOKEN" \
-H "Content-Type: CONTENT_TYPE" \
-X POST \
-d '[{"email": "jason@yourcompany.com", "date": "8/16/2016", "calls": "3"}]' 
\
https://endpoint/api/v1/data/DATA_TYPE/

I have my auth token, my endpoint URL and my content type is JSON, which can be seen in my code below. Also, I have an array instead of the example for the body above.

and here's the affected part of my code:

 //new array specifically for the final JSON file
 $content2 = [];

 //creating array for new fetch since it now has the updated extension IDs
 while ($d2 = mysqli_fetch_array($data2, MYSQLI_ASSOC)) {

     // Store the current row
     $content2[] = $d2;

 }


 // Store it all into our final JSON file
 file_put_contents('ambitionLog.json', json_encode($content2, JSON_PRETTY_PRINT ));


 //Beginning code to upload to Ambition API via POST

 $url = 'endpoint here';

 //Initiate CURL
 $ch = curl_init($url);

 //JSON data
 $jsonDataEncodeUpload = json_encode($content2, JSON_PRETTY_PRINT);

 //POST via CURL
 curl_setopt($ch, CURLOPT_POST, 1);

 //attach JSON to post fields
 curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncodeUpload);

 //set content type
 curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));

 //execuate request
 $postResult = curl_exec($ch);

So, like I said, nothing about the file or the data needs to be changed, I just need to have this cURL section take the existing array that's being written to a JSON file and upload it to the API via post. I just need help making my php syntax for curl match the command line example.

Thanks for any possible help.

Geoff_S
  • 3,969
  • 3
  • 19
  • 64

2 Answers2

1

Have you tried with file_get_contents ( http://en.php.net/file_get_contents ).

$postdata = http_build_query(
    array(
        'var1' => 'some content',
        'var2' => 'doh'
    )
);

$opts = array('http' =>
    array(
        'method'  => 'POST',
        'header'  => 'Content-type: application/x-www-form-urlencoded',
        'content' => $postdata
    )
);

$context  = stream_context_create($opts);

$result = file_get_contents('http://example.com/submit.php', false, $context);

I have found the answer on stackoverflow How to post data in PHP using file_get_contents?

Mike X
  • 129
  • 1
  • 7
1

Here is worked example of code. Check $err may be it will be helpful.

        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_TIMEOUT, 5);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $_POST('data'));
        curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type:application/json']);

        $result = curl_exec($ch);
        $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        $err = curl_error($ch);
        curl_close($ch);
0x000f
  • 1,360
  • 1
  • 9
  • 8