I have collected data and created and array :
Array
(
[0] => Array
(
[id] => 1
[name] => Martin
[surname] => test
[email] => [email protected]
[dob] => 2015-02-24
)
[1] => Array
(
[id] => 2
[name] => Kary
[surname] => paulman
[email] => [email protected]
[dob] => 2015-06-26
)
)
I have multiple records in this array.
I want to post each record in the array to www.recieve.com , where it will pass a response of 'true' if post was successful and 'false' if it fails.
I have researched the interent and i dont even know where to start.
So far my code looks like this (this is just for the array)
$query = "SELECT * FROM applicants";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_assoc($result)){
$res[] = $row;
}
echo "<pre>"; print_r($res); echo "</pre>";
I have tryed this and it is not working :
//Build my array
$query = "SELECT * FROM applicants";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_assoc($result)){
$res[] = $row;
}
//URL to post to
$url = 'https://theurl.com?';
//url-ify the data for the POST
foreach($res as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
There are 2 problems with your cURL request that I can see:
$fields is undefined.You can solve that using for example:
// make sure the values are encoded correctly:
$fields_string = http_build_query($res);
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
// you need the count of your `$res` variable here:
curl_setopt($ch,CURLOPT_POST, count($res));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
Also note that you don't need a question mark at the end of the url. I don't know if that would cause problems, but you should probably just remove that:
$url = 'https://theurl.com';
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With