0

My CSV has a single column with 40000 phone numbers

The following code read the CSV column and is fast

$dataArray = csvstring_to_array( file_get_contents('test.csv'));

My CURL code looks like this

$ch = curl_init();

     $data = http_build_query($dataArray);

    curl_setopt($ch, CURLOPT_POSTFIELDS, $dataArray);
    curl_setopt($ch, CURLOPT_URL, "https://api.theblacklist.click/standard/api/v1/bulkLookup/key/[APIKEY]/response/json");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);

    $output = curl_exec($ch);

The JSON parameters expected by the API look like the following. I am stuck about how to pass the "phones":[ and then pass the CSV Data array as a parameter to the curl/API?

curl -XGET ''
  -H 'Content-Type: application/json' 
  -d'
{
  "phones":[
  "15555558353",
  "15555555555",
  "15555552740",
  "15555552741",
  "15555552738"
  ]
}'
user580950
  • 3,558
  • 12
  • 49
  • 94
  • `$dataArray = ["phones" => csvstring_to_array(...)]` – Justinas Jan 22 '20 at 18:50
  • Does this answer your question? [How do I use arrays in cURL POST requests](https://stackoverflow.com/questions/13596799/how-do-i-use-arrays-in-curl-post-requests) – Justinas Jan 22 '20 at 18:50
  • @Justinas I tried $dataArray = ["phones" => csvstring_to_array( file_get_contents('test.csv'))]; it returns blank Array – user580950 Jan 22 '20 at 18:55

1 Answers1

1

It seems the API server expect JSON-formatted data to be passed as POST body

$dataArray = csvstring_to_array(file_get_contents('test.csv'));
$jsonString = json_encode(['phones' => array_values($dataArray)]);

curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonString);
Anggara
  • 625
  • 4
  • 8
  • 1. When I use this print_r($dataArray); it prints the data 2. when I print this $jsonString = json_encode(['phones' => array_values($dataArray)]); echo "JSON>>>>".$jsonString; It is Blank? – user580950 Jan 22 '20 at 19:09
  • What version of PHP are you using? Can you use `print_r($jsonString);` instead? – Anggara Jan 22 '20 at 19:14
  • php 7, echo print_r($jsonString); returns 1 – user580950 Jan 22 '20 at 19:15
  • You don't need `echo` before `print_r`.What is the content of `$dataArray`? Is it like `['phone1', 'phone2', 'phone3']` ? – Anggara Jan 22 '20 at 19:17
  • Just blank? Can you print something after it? It might caused by PHP error. You may want to enable error reporting: `error_reporting(E_ALL); ini_set('display_errors',1);` at the beginning of script – Anggara Jan 22 '20 at 19:24
  • Array ( [phones] => Array ( ) ) that is displayed – user580950 Jan 22 '20 at 19:26
  • Weird, that means `$dataArray` contains empty array. Did you put `$dataArray = ...` declaration in the same scope of `$jsonString = ...` declaration? (e.g: not in a function) – Anggara Jan 22 '20 at 19:32
  • That seems correct. Maybe `array_values` messed it up. Try `json_encode(['phones' => $dataArray]);` – Anggara Jan 22 '20 at 19:43
  • all blank not sure what is happening – user580950 Jan 22 '20 at 20:10
  • on the same file, can you confirm `print_r($dataArray)` has value? or you misplaced `test.csv`? – Anggara Jan 22 '20 at 20:13