POST cURL Body from File: PHP Code Examples ( JSON, CSV )

Steps to POST cURL request with a body from a File in PHP Sending a CURL request with body from file in PHP requires a few steps. You need to retrieve the content from your files, setup your curl request with curl_init, and include the CURLOPT_POSTFIELDS to the body content before executing with curl_exec. Initialize your cURL request with curl_init Open up your file content with file_get_contents Convert the file contents to a PHP array if not already. Setup your cURL Request Options array. Set CURLOPT_POST to true in options Set CURLOPT_POSTFIELDS to your PHP array from above Pass the options array to curl_setopt_array Send the cURL request with curl_exec Close the cURL request with curl_close Verify and validate response Continue processing. Post cURL Body from File in PHP Code Example <?php //Initialise the cURL var $curl = curl_init(); //Create a POST array with the file in it $fileData =…

read more

Recursive Loop Multidimensional JSON Arrays in PHP | GeoJSON

json array with PHP
How to Iterate JSON Arrays in PHP Using a Recursive Function with GeoJSON Load and set your GeoJSON content into a PHP variable Use json_decode to convert the JSON content into a php multidimensional array Create a custom recursive function to iterate the JSON content Run a while loop inside the custom function and call itself when it has children Run a foreach loop on your root array from your geojson content Create a new RecursiveArrayIterator class inside the loop Call your custom recursive function and pass in your RecursiveArrayIterator class Continue working and processing your GeoJSON array PHP Code Example: Loop Through a Multidimensional JSON Array using recursion and a foreach loop with GeoJSON //Fetches the GeoJSON array. $geojson = file_get_contents("sampleGeoJSON.json",false); //Converts GeoJSON into an associative array. $geojson_array = json_decode($geojson,true); //A recursive function to traverse the GeoJSON array. function traverseGeoJSON($iterator) { //If there is a property left. while (…

read more