CURL request inside snippet instantly outputs JSON response to page

Using Modx 2.8.4, I have a simple search form (employing Formit and FormitRetriever). After submitting the form, the user is redirected to a results page (which works fine) where I integrated a simple snippet into the page template like this:

[[!apicalls? &query=[[!+fi.search]]]]

This is the code of my snippet which simply queries an HTTP API endpoint to gather JSON data:

<?php

$ch = curl_init('https://www.foo.com/?search='.$query);
    curl_setopt(...);
    $apiresult = curl_exec($ch);
curl_close($ch);

return "foostring";

What happens with this code is, that the content of $apiresult (= the entire JSON string) is instantly rendered into the page and I don’t know why since I am not returning it!? Before outputting, I’d like to perform further processing on the JSON data (like extracting certain fields etc.). Why does the snippet render the response into the page? How can that be avoided? You can easily tell that I just recently started developing my own snippets and still keep struggling with the very basics =(

Try the following:

<?php
$ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "https://www.foo.com/?search='.$query");
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $apiresult = curl_exec($ch);
curl_close($ch);

return "foostring";

And changing the last line to: return $apiresult; should return your curl content.

That was the solution. The return "foostring"; was just for debugging purposes to make sure I return nothing even close to the request response. Thanks again.

1 Like

Glad you got it sorted :+1:

This topic was automatically closed 2 days after discussion ended and a solution was marked. New replies are no longer allowed. You can open a new topic by clicking the link icon below the original post or solution and selecting “+ New Topic”.