Issues with php form using Curl

782

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

897

Answer

Solution:

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); ?>
309

Answer

Solution:

$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);

People are also looking for solutions to the problem: php - class java.lang.String can not be converted to an Long while DynamoDB creating table

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.