3

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);
2
  • 5
    Use cURL. Commented Jun 8, 2015 at 13:34
  • If you use composer or something i would go for Guzzle. But curl would also work Commented Jun 8, 2015 at 13:41

2 Answers 2

1

There are 2 problems with your cURL request that I can see:

  1. You are not encoding the values correctly for use in a query string
  2. $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';
Sign up to request clarification or add additional context in comments.

Comments

0

Use Curl and like this

$ch = curl_init();                    // initiate curl
$url = "http://www.somesite.com/curl_example.php"; // where you want to post data
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, true);  // tell curl you want to post something
curl_setopt($ch, CURLOPT_POSTFIELDS, "var1=value1&var2=value2&var_n=value_n"); // define what you want to post
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // return the output in string format
$output = curl_exec ($ch); // execute

curl_close ($ch); // close curl handle

var_dump($output); // show output

?>

Use your array in the : curl_setopt($ch, CURLOPT_POSTFIELDS

Comments

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.