1

I'm trying to write the array $jsonDataInArray to an external csv file. Right now, my file only has the column headers without the data underneath. Could someone help me step through this PHP array, $jsonDataInArray, and write it to an external .csv file?

//set url for pipedrive data being pulled
$api_token="soemToken";
$url = "https://someURL.com;

$ch = curl_init(); //initialize connection with a URL
//check if cURL is enabled or not
if(is_callable('curl_init'))
{
    echo "curl_init Enabled";
}
else
{
    echo "curl_init Not enabled";
}
echo '<br/><br/><br/><br/><br/><br/>';
curl_setopt($ch, CURLOPT_URL, $url); //fetching URL
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //return queried data as string
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);//verify certificate
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);//check the existence of a common name & verify that it matches the hostname provided
curl_setopt ($ch, CURLOPT_CAINFO, dirname(__FILE__)."/permissingFile.pem");//name of file holding certificates to verify peer with
$json_response = curl_exec($ch);//perform cURL session. Returns ALL of JSON data if sucessful, false if not. 
$info = curl_getinfo($ch);//gets array of info about cURL transfer.
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);//gets HTTP message about cURL transfer
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));
    //die();
}
curl_close($ch);//close connection with URL

// create an array from the data that is sent back from the API
$response = json_decode($json_response, 1);

// Gets the count of records returned from the api. Used in the for loop to go through response 1 array element at a time. 
$count = Count($response['data']);

for ($x=0; $x<$count; $x++)
{
    $currentRecord = $response['data'][$x];
    $jsonDataInArray = array
    (
        "id" => $response['data'][$x]['id'],
        "user_id" => $response['data'][$x]['user_id']['id'],
        "person_id" => $response['data'][$x]['person_id']['value'],
        "org_id" => $response['data'][$x]['org_id']['value'],
        "stage_id" => $response['data'][$x]['stage_id'],
        "title" => $response['data'][$x]['title'],
        "value" => $response['data'][$x]['value'],
        "currency" => $response['data'][$x]['currency'],
        "add_time" => $response['data'][$x]['add_time'],
        "update_time" => $response['data'][$x]['update_time'],
        "stage_change_time" => $response['data'][$x]['stage_change_time'],
        "active" => $response['data'][$x]['active'],
        "deleted" => $response['data'][$x]['deleted'],
        "status" => $response['data'][$x]['status'],

    );
    ksort($currentRecord);

}

$test_array = $response['data'][0];//test_array = first row of data

if($startPos == 0){
    $fp = fopen('cacheDeals3.csv', 'w');
    fputcsv($fp, array_keys($response['data'][0]));
}else{
    $fp = fopen('cacheDeals3.csv', 'a');
}


foreach ($jsonDataInArray as $fields)
{
    fputcsv($fp, $fields);
}

1 Answer 1

1

$jsonDataInArray is being overwritten on every iteration of the for loop, therefore fputcsv is being passed a string as the $field parameter rather than an array.

You need to append a new array to $jsonDataInArray each time, change $jsonDataInArray = array

to

$jsonDataInArray[] = array

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.