0

I have created a php script to get csv file first row and getting data from curl but it only show last id's data. Here is my code:

$row = 1;
if (($handle = fopen("file1.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        
        $id = $data[0]. "<br />";

        

    }

    fclose($handle);
}

$entity = getData($id);
    if($entity->success){
    $websites = $entity->data->websites;
    $web = $websites[0];
    $website_address = $web->website_url;
    echo "Website Address: $website_address <br>";
}



function getData($id){
    $api_endpoint = 'url';
    $data = array();
    $data['api_key'] = 'abcdef';   
    $data['e_id'] = $id;
    $url = $api_endpoint."?".http_build_query($data);

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    curl_setopt($ch, CURLOPT_POST, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    $output = curl_exec($ch);
    curl_close($ch);
    $obj= json_decode($output);

    return $obj;

}

my csv data is like:

960626888790016
437728935677953
960626888790016
438798134112256
999649717096448

But after getting curl data from api I only get last id's data. I want all data separately.

0

1 Answer 1

1

Use following to invoke cURL for each of your id's in the csv file.

  1. Build an array $ids with the id's from the csv file
  2. foreach() to invoke getData() and run cURL on each seperate id

.

<?php
$row = 1;
if (($handle = fopen("file1.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        $ids[] = $data;
    }
    fclose($handle);
}
//flatten the array with id's
$ids = call_user_func_array('array_merge', $ids);
// cURL for each id
foreach($ids as $id) {
    $entity = getData($id);
    if ($entity->success) {
        $websites = $entity->data->websites;
        $web = $websites[0];
        $website_address = $web->website_url;
        echo "Website Address: $website_address <br>";
    }
}


function getData($id){
    $api_endpoint = 'url';
    $data = array();
    $data['api_key'] = 'abcdef';
    $data['e_id'] = $id;
    $url = $api_endpoint."?".http_build_query($data);

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    curl_setopt($ch, CURLOPT_POST, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    $output = curl_exec($ch);
    curl_close($ch);
    $obj= json_decode($output);

    return $obj;

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

5 Comments

i only need the first row of data: $id = $data[0]. "<br />"; but if i use this got some errors: Warning: array_merge(): Expected parameter 1 to be an array, string given in /opt/lampp/htdocs/readCSV/test.php on line 11
This is working code - it builds an array with the id's to run through cURL one at a time, but if that is not your requirement I'll delete the answer. If you want to just run cURL for one id, e.g. $ids[0], then delete the foreach() loop and just feed getData[$ids[0])...
there are several rows in csv file thats why I only need the first row so this first row have ids only but you wrote $data only it returns all data, i need only first row of csv file do you got my point?
I've taken your sample csv data verbatim, maybe I shouldn't have... See if you get it to work, otherwise I'll delete and leave it to someone else.
@shawn consider accepting and/or up-voting the answer if it helped you :)

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.