Jump to content

Recursive function file_put_contents file header issue


jarvis

Recommended Posts

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

Link to comment
Share on other sites

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 by jarvis
typo in code
Link to comment
Share on other sites

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.

 

  • Like 1
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.