Question:
How to Read a csv file with php using cURL

PHP's cURL extension is typically used for making HTTP requests, and it's not the most straightforward option for reading a CSV file directly. Instead, you might want to use native file functions or specialized libraries for handling CSV files in PHP. However, if you have a specific use case where you need to fetch the CSV file using cURL, and then process it.


<?php

/*

Download csv file with php using cURL

*/


//  URL SharePoint

$url = 'https://1drv.ms/u/s!AtG6qXRVeUjAgvoLItVayTTPAYhnAw?e=AzuB2G';


/* Ini */

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);

/* Result as string */

curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);

/* Download the file from the URL */

$output = curl_exec($ch);

/* Data file to array */

$csv = array_map('str_getcsv', file($url));

/* Counting the records */

$nRegistros = count($csv);

/* Print */

echo $nRegistros."\n";

print_r($csv);

?>


If you want to simplify the process you can use the file_get_contents() function directly with a URL instead of cURL: PHP documentation


Suggested blogs: 

>How to do wild grouping of friends in Python?

>What to do when MQTT terminates an infinite loop while subscribing to a topic in Python?

>What makes Python 'flow' with HTML nicely as compared to PHP?

>How to do Web Scraping with Python?


Ritu Singh

Ritu Singh

Submit
0 Answers