The application Pipedrive gives me inconsistent json data. For example, in some array elements, it gives me "formatted_value":"$3,500","weighted_value":2100,"formatted_weighted_value":"$2,100","rotten_time":null
, while in others, it gives me, "formatted_value":"$2,950","rotten_time":null,"weighted_value":2950,"formatted_weighted_value":"$2,950"
. I would like the json data to be in the order of formatted_value,weighted_value,formatted_weighted_value,rotten_time
in every array element, but that's not the case sadly.
Does anyone know of a way to check that the right data is written the right column based on column name and key name?
Below is my code to parse the json data:
function parseFunction($startPos) {
$url = 'urlToCallJsonData';
$ch = curl_init($url); //initialize connection with a URL
if(is_callable('curl_init'))
{
echo "Enabled";
}
else
{
echo "Not enabled";
}
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt ($ch, CURLOPT_CAINFO, dirname(__FILE__)."/cacert.pem");
$json_response = curl_exec($ch);
$info = curl_getinfo($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ( $status != 200 )
{
die("Error: call to URL $url failed with status $status, response $json_response, curl_error " . curl_error($ch) . ", curl_errno " . curl_errno($ch));
}
curl_close($ch);
$response = json_decode($json_response, true);
$count = Count($response['data']);
for ($x=0; $x<$count; $x++)
{
$currentRecord = $response['data'][$x];
}
//open writing to file
if($startPos == 0)
{
$fp = fopen('cacheDeals.csv', 'w');
}
else
{
$fp = fopen('cacheDeals.csv', 'a');
}
$test_array = $response['data'][0];//test_array = first row of data
// writes the headers to the csv file.
if($startPos == 0)
{
$keys = array_keys($test_array);
fputcsv($fp, $keys);
}
$array_records = $response['data'];//all of incoming data
//write data to csv file
foreach ($array_records as $fields)
{
fputcsv($fp, $fields);
}
//check to see if more data should be written
$more = $response[additional_data][pagination][more_items_in_collection];
$nextStart = $response[additional_data][pagination][next_start];
if($more =="true")
{
downloadPipedriveDealsData($nextStart);
}
}//end of function
parseFunction(0);