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"
  ]
}'
3
  • $dataArray = ["phones" => csvstring_to_array(...)] Commented Jan 22, 2020 at 18:50
  • Does this answer your question? How do I use arrays in cURL POST requests Commented Jan 22, 2020 at 18:50
  • @Justinas I tried $dataArray = ["phones" => csvstring_to_array( file_get_contents('test.csv'))]; it returns blank Array Commented Jan 22, 2020 at 18:55

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

10 Comments

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?
What version of PHP are you using? Can you use print_r($jsonString); instead?
php 7, echo print_r($jsonString); returns 1
You don't need echo before print_r.What is the content of $dataArray? Is it like ['phone1', 'phone2', 'phone3'] ?
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
|

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.