0

Issues with php form using Curl. The below code works fine as stated

but when i wants to post the address from form fields by adding the followings, not seems to work

$data = array(
        'address' => $_POST['address']

);

curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);

can anyone help

1
  • You need to send it as a query (i.e ?address=ADDRESS_HERE&fields..) - http_build_query() Commented Jul 18, 2014 at 5:50

2 Answers 2

1

try to send your post data as a query string

$str = '';
foreach($data as $key => $val){
  $str.= $key.'='.$val.'&';
}
rtrim($str,'&');
curl_setopt($ch, CURLOPT_POSTFIELDS, $str);

or

curl_setopt($ch, CURLOPT_POSTFIELDS, 'address='.$_POST['address']);

or

curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));

According comment error you need code:-

<?php 
$address = "Capitol Avenue, Elsmere, Kentucky"; 
$address = str_replace(" ", "+", $address); 
$address_url = "https://maps.googleapis.com/maps/api/geocode/json";
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $address_url.'?address='.$address); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); 
echo $response = curl_exec($ch);
print_r(curl_getinfo($ch)); 
curl_close($ch); 
$response_a = json_decode($response); ?>
Sign up to request clarification or add additional context in comments.

2 Comments

i have tried the above option but not working. here is my entire code
<?php error_reporting(0); $address=$_POST['address']; //$address = "Capitol Avenue, Elsmere, Kentucky"; $address = str_replace(" ", "+", $address); $address_url = "maps.google.com/maps/api/geocode/…"; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $address_url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_PROXYPORT, 3128); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); $response = curl_exec($ch); curl_close($ch); $response_a = json_decode($response); ?>
0
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL,"http://www.example.com/post.php");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
            "address=$_POST['address']");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);

$server_output = curl_exec ($ch);

curl_close ($ch);

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.