0

Is my PHP cURL request malformed because of using a variable in the URL The request works fine when I check it without that Variable. How can i fix it?

<?php

$search_string = $_POST["search_string"];

$curl = curl_init();

curl_setopt_array($curl, array(
    CURLOPT_URL => "https://maps.googleapis.com/maps/api/place/textsearch/json?query=" . $search_string . "&key=AIzaSyDsb0EFY9lde2EcTYKou3Xrz9ax91qiuTY",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_CUSTOMREQUEST => "GET"
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
    echo "cURL Error: " . $err;
} else {
    echo $response;
}

?>
8
  • 1
    var_dump($search_string); to confirm the value is what you expect Commented Aug 8, 2019 at 13:11
  • What are you actually seeing? Commented Aug 8, 2019 at 13:12
  • 3
    urlencode the search string, for one. Commented Aug 8, 2019 at 13:14
  • @Jonnix: The Google Answer ist "INVALID REQUEST" Commented Aug 8, 2019 at 13:54
  • @PatrickQ var_dump and urlencode has no effect Commented Aug 8, 2019 at 13:57

1 Answer 1

1
  1. Please define constant:
define('MYURL',   "https://maps.googleapis.com/maps/api/place/textsearch/json?query=" . $search_string . "&key=AIzaSyDsb0EFY9lde2EcTYKou3Xrz9ax91qiuTY");
  1. Change CURLOPT_URL to: CURLOPT_URL => MYURL

This works for me.

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

2 Comments

I fail to see how that would resolve the problem. You're just aliasing a variable to the string and passing it at a parameter. If the issue is with the URL or a cURL variable to begin with, this wouldn't solve anything.
It's magic :) Cant explain.

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.