Search code examples
phpcurlgoogle-custom-search

Why is my REST request to Google's custom search engine API returning a 400 error?


When I perform the request, I am met with a 400 error stating that "our client has issued a malformed or illegal request. That’s all we know.". All of my keys are valid and the website's url that I'm using to issue the request has been verified. When I input the url into my browser, it gives me a perfect response full of relevant data.

Code Snippet:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://www.googleapis.com/customsearch/v1?
key=SERVER_KEY&cx=SEARCH_ENGINE_ID&q=flower&searchtype=image");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$file = curl_exec($ch);
curl_close($ch); 

echo $file;

Solution

  • It was a sneaky syntactical error all along. My text editor was auto-spacing after the SERVER_KEY, which made the API request null