jarvis Posted August 13, 2021 Share Posted August 13, 2021 Hi All, I've a function that pulls data from a paginated API. The function is called recursively in order to work through each page and get the data. Within the function, I use file_put_contents to write the information to a CSV file. What I'd like to do, is add a first row/header to the CSV file with column names. I'm probably being daft but if I add the header information in, it gets repeated on each iteration of the loop. Here's my code: function get_from_api() { #$page_report = get_stylesheet_directory() . '/page-report.txt'; $filename = 'products.csv'; $product_data = get_stylesheet_directory() . '/'.$filename; $current_page = ( ! empty( $_POST['current_page'] ) ) ? $_POST['current_page'] : 1; $products = []; // Should return an array of objects $results = wp_remote_retrieve_body( wp_remote_get('www.domain.com/batch.php?page=' . $current_page . '&per_page=10') ); // create a file to test the output $data = 'Current Page: '.$current_page; #file_put_contents( $page_report, $data . "\n\n", FILE_APPEND ); // turn it into a PHP array from JSON string $results = json_decode( $results ); // Either the API is down or something else spooky happened. Just be done. if( ! is_array( $results ) || empty( $results ) ){ return false; } #print_r($results); $products[] = $results; foreach( $products[0] as $product ){ $result .= $product->id. ','; $result .= $product->name; file_put_contents( $product_data, $result . "\n", FILE_APPEND ); } $current_page = $current_page + 1; wp_remote_post( admin_url('admin-ajax.php?action=get_from_api'), [ 'blocking' => false, 'sslverify' => false, // we are sending this to ourselves, so trust it. 'body' => [ 'current_page' => $current_page ] ]); } Any help would be great Thanks Quote Link to comment Share on other sites More sharing options...
requinix Posted August 13, 2021 Share Posted August 13, 2021 So in other words, you only want to write the header line if you've started writing to this file from scratch? Quote Link to comment Share on other sites More sharing options...
jarvis Posted August 13, 2021 Author Share Posted August 13, 2021 Yes that's right. So the first iteration adds the header Quote Link to comment Share on other sites More sharing options...
requinix Posted August 13, 2021 Share Posted August 13, 2021 No no, forget "iteration". Focus only on the "if you've started writing to this file from scratch" part. What, exactly, would "from scratch" mean? Is there any way you could tell when that's the case? Quote Link to comment Share on other sites More sharing options...
jarvis Posted August 13, 2021 Author Share Posted August 13, 2021 Would it be when you declare the file? I'm clearly missing something because surely being recursive, it will just keep adding to the file Quote Link to comment Share on other sites More sharing options...
requinix Posted August 13, 2021 Share Posted August 13, 2021 "Declare the file"? A much bigger hint so I can go to bed: the first time you run this code, will the file exist? Quote Link to comment Share on other sites More sharing options...
jarvis Posted August 13, 2021 Author Share Posted August 13, 2021 (edited) No, the file doesn't exist - which I get. I can't see how to add something to the file without it being repeated, as this is the issue I keep getting. I switched from file_put_contents to fopen and have achieved what I need. Why it doesn't work using file_put_contents I don't know $products[] = $results; $data = array(); if( $current_page == 1 ){ $data[] = array( 'id', 'name' ); } foreach( $products[0] as $product ){ $result .= $product->id. ','; $result .= '"'.$product->name.'"'; $data[] = array( 'id' => $product->id, 'name' => $product->name ); file_put_contents( $product_data, $result . "\n", FILE_APPEND | LOCK_EX ); $i++; } $file = fopen($product_data2,"a"); foreach ($data as $line) { fputcsv($file, $line); } fclose($file); Edited August 13, 2021 by jarvis typo in code Quote Link to comment Share on other sites More sharing options...
Phi11W Posted August 13, 2021 Share Posted August 13, 2021 2 hours ago, jarvis said: I can't see how to add something to the file without it being repeated, as this is the issue I keep getting. At the point in your function where you're about to append some data into the file, test whether the file exists. If the file does not exist, then write the column headers into the file. Then, append the data into the file, regardless of whether you just wrote the headers or not. In the second and subsequent calls (recursive or not), the file will exist and so the headers will not be repeated. Regards, Phill W. 1 Quote Link to comment Share on other sites More sharing options...
jarvis Posted August 13, 2021 Author Share Posted August 13, 2021 Ah! Penny drops - many thanks @Phi11W Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.