I am trying to submit a form using curl in PHP. is there any web to get the name of fields on target URL so that I can map them to my fields. Below is my code, I am using for curl:
<form action="" method="post">
<input type="text" name="name" >
<input type="text" name="email">
<input type="submit" name ="create_account">
</form>
<?php
if(isset($_POST['create_account'])){
$name = $_POST['name'];
$email = $_POST['email'];
$url = "www.exampleurl.com"; // Where you want to post data
try {
$ch = curl_init();
if (FALSE === $ch)
throw new Exception('failed to initialize');
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true); // Tell cURL you want to post something
curl_setopt($ch, CURLOPT_POSTFIELDS, "name=". $name ."&email=". $email);
// Define what you want to post
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//curl_setopt(/* ... */);
$content = curl_exec($ch);
$info = curl_getinfo($ch);
$json = json_decode($content, true);
if (FALSE === $content)
throw new Exception(curl_error($ch), curl_errno($ch));
} catch(Exception $e) {
trigger_error(sprintf(
'Curl failed with error #%d: %s',
$e->getCode(), $e->getMessage()),
E_USER_ERROR);
}
}
?>
Try this:
$data = array('name'=>$name, 'email'=>$email);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
From php5 upwards, usage of http_build_query is recommended.
Before php5 you could perform urlencode() to each var value.