2

I seem to have run into a problem with trying to submit form variables to multiple urls. The code i have below takes variables from a form and stores them in php, then I am trying to send those variables to certain urls (the variables do pass through, I've tested that). I heard cUrl is the way to do it, but I don't know if its working cause im stumped on how to to retrieve the response code.

<?php

$name = $_POST['firstname'];
$email = $_POST['email'];
$src = $_POST['srcUrl'];
$ip= $_SERVER['SERVER_ADDR'];
$suDate = date('Y-m-d H:i:s');


$data = array(
"fn" => $name,
"src" => $src,
"em" => $email,
"ip" => $ip,
"signupDate" => $suDate
);


$data2 = array(
"firstname" => $name,
"email" => $email,
);

function post_to_url($url, $data) {
$fields = '';
foreach($data as $key => $value) { 
  $fields .= $key . '=' . $value . '&'; 
}
rtrim($fields, '&');

$post = curl_init();

curl_setopt($post, CURLOPT_URL, $url);
curl_setopt($post, CURLOPT_POST, count($data));
curl_setopt($post, CURLOPT_POSTFIELDS, $fields);
curl_setopt($post, CURLOPT_RETURNTRANSFER, 1);

$result = curl_exec($post);

curl_close($post);
}



post_to_url("http://exampleURL.com/page.php", $data);
post_to_url("http://exampleURL2.com/cgi-bin/add.cgi", $data2);



print_r($result);


?>

any help is greatly appreciated. thanks

0

2 Answers 2

2

In the code example you've provided above, $result contains the response you're looking for. To view the full details of the variable, use this:

print_r($result);

print_r is useful when you're not sure what data type a variable is or what data it might contain. print_r gives you all available information on the variable.

Edit: Also change curl_setopt($post, CURLOPT_POST, count($data)); to curl_setopt($post, CURLOPT_POST, 1); CURLOPT_POST is a true/false option (http://php.net/manual/en/function.curl-setopt.php).

Sign up to request clarification or add additional context in comments.

3 Comments

Hi, i have included the 'print_r($result);' to get response, but still nothing is showing. I have updated the code to reflect the changes, including submission to to a 2nd url. WIll it still work? thanks, for the help
@David Maldonado If it's still blank, then you're not getting a response. Try setting CURLOPT_POST to 1, instead of the count().
this worked really well, thanks for the help. I have one other random question how can i add a little bit more to this script by executing one of those 'post_to_url' functions randomly. ie, hits the script and executes function at random? thanks for the help
1

Inside your function where you have $result = curl_exec($post); the $result data is the response. So you need to add return $return; and call the function with $response = post_to_url(...); and echo the response.

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.