John Davidson

php - Multiple CURLOPT_POSTFIELDS data payloads

0 comments
Message:


Here is what I have done so far. I make a curl request and pass a json to the CURLOPT_POSTFIELDS fields. This works alright. The problem is I want the code to run for multiple users which are in a json


<?php

$curl = curl_init();

curl_setopt_array($curl, array(
CURLOPT_URL => 'http://www.url.com',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS =>'{
"customerName":"Eugene Afet",
"mno":"MTN",
"amount":1,
"msisdn":"233546888444",
"description":"Payment For Tickets at My shop",
"reference":"kRybiV4lllVwddlly3N2BW1"
},
{
"customerName": "james",
"mno": "MTN",
"amount": 1,
"msisdn": "2330987654",
"description": "Awaiting",
"reference": "0asa0wwww00aaabfbsubfhbj"
}',
));

$response = curl_exec($curl);

curl_close($curl);
echo $response;

What works, when the json is only one. I have read about curl_multi_exec but I am not sure how to apply it here.


<?php

$curl = curl_init();

curl_setopt_array($curl, array(
CURLOPT_URL => 'http://www.url.com',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS =>'{
"customerName":"Eugene Afet",
"mno":"MTN",
"amount":1,
"msisdn":"233546888444",
"description":"Payment For Tickets at My shop",
"reference":"kRybiV4lllVwddlly3N2BW1"
}',
));

$response = curl_exec($curl);

curl_close($curl);
echo $response;


My Answer:
To send multiple payloads in a single curl request, you can use the `curl_multi_init()` function to create a multi-handle and then add multiple individual curl handles to it. Here is an example of how you can modify your code to send multiple payloads:

php

$payloads = array(
array(
"customerName" => "Eugene Afet",
"mno" => "MTN",
"amount" => 1,
"msisdn" => "233546888444",
"description" => "Payment For Tickets at My shop",
"reference" => "kRybiV4lllVwddlly3N2BW1"
),
array(
"customerName" => "james",
"mno" => "MTN",
"amount" => 1,
"msisdn" => "2330987654",
"description" => "Awaiting",
"reference" => "0asa0wwww00aaabfbsubfhbj"
)
);

$mh = curl_multi_init();
$handles = array();

foreach ($payloads as $payload) {
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'http://www.url.com',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => json_encode($payload),
));
curl_multi_add_handle($mh, $curl);
$handles[] = $curl;
}

$active = null;
do {
$mrc = curl_multi_exec($mh, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);

while ($active && $mrc == CURLM_OK) {
if (curl_multi_select($mh) != -1) {
do {
$mrc = curl_multi_exec($mh, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);
}
}

foreach ($handles as $handle) {
$response = curl_multi_getcontent($handle);
echo $response . "\n";
curl_multi_remove_handle($mh, $handle);
curl_close($handle);
}

curl_multi_close($mh);


In this code snippet, we create an array of payloads, loop through each payload to create individual curl handles, add them to the multi-handle, and then execute the requests using `curl_multi_exec()`. Finally, we retrieve the responses for each handle and close the handles.

Rate this post

5 of 5 based on 8627 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us