jaybo Posted March 10, 2022 Share Posted March 10, 2022 I am running a script with a curl GET request but my code is not currently outputting anything. Here is the code, I think that I need to add the parameters to the url but need some direction as how to do that? Here is the code; <?php $curl = curl_init(); // Array Parameter Data $data = [ 'lat'=>'52.509120', 'lon'=>'-1.884915', ]; curl_setopt_array($curl, [ CURLOPT_URL => "https://geo.fcc.gov/api/census/area", CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => "", CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 30, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => "GET", CURLOPT_POSTFIELDS => $data, CURLOPT_HTTPHEADER => [ "Content-Type: application/json", ], ]); $response = curl_exec($curl); $err = curl_error($curl); curl_close($curl); if ($err) { echo "cURL Error #:" . $err; } else { echo $response; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/314592-php-curl-get-request-with-parameters/ Share on other sites More sharing options...
ginerjm Posted March 10, 2022 Share Posted March 10, 2022 (edited) I think you have an error in your options array. An extra comma at the end perhaps. CURLOPT_HTTPHEADER => [ "Content-Type: application/json", ], <<<<< ]); If you have error checking enabled and on I would have thought this to be caught. Edited March 10, 2022 by ginerjm Quote Link to comment https://forums.phpfreaks.com/topic/314592-php-curl-get-request-with-parameters/#findComment-1594357 Share on other sites More sharing options...
jaybo Posted March 10, 2022 Author Share Posted March 10, 2022 Thanks for spotting that ginerjm - still no luck however. The error is now as follows; SyntaxError: Unexpected token - at parse (/var/app/current/node_modules/body-parser/lib/types/json.js:83:15) at /var/app/current/node_modules/body-parser/lib/read.js:116:18 at invokeCallback (/var/app/current/node_modules/raw-body/index.js:262:16) at done (/var/app/current/node_modules/raw-body/index.js:251:7) at IncomingMessage.onEnd (/var/app/current/node_modules/raw-body/index.js:307:7) at emitNone (events.js:86:13) at IncomingMessage.emit (events.js:185:7) at endReadableNT (_stream_readable.js:974:12) at _combinedTickCallback (internal/process/next_tick.js:80:11) at process._tickCallback (internal/process/next_tick.js:104:9) Quote Link to comment https://forums.phpfreaks.com/topic/314592-php-curl-get-request-with-parameters/#findComment-1594358 Share on other sites More sharing options...
Solution kicken Posted March 10, 2022 Solution Share Posted March 10, 2022 24 minutes ago, jaybo said: I think that I need to add the parameters to the url but need some direction as how to do that? Yes. CURLOPT_POSTFIELDS is only for doing POST requests. For GET you just include the parameters in the URL's query string. You do that by adding a ? followed by the parameters in name=value&name=value2 format with proper url encoding applied. PHP has a function to build the appropriate string from an array of parameters: http_build_query. Use that and concatenate the result to the URL. 'https://geo.fcc.gov/api/census/area?'.http_build_query($data); Quote Link to comment https://forums.phpfreaks.com/topic/314592-php-curl-get-request-with-parameters/#findComment-1594359 Share on other sites More sharing options...
benanamen Posted March 10, 2022 Share Posted March 10, 2022 (edited) * Edited March 10, 2022 by benanamen Quote Link to comment https://forums.phpfreaks.com/topic/314592-php-curl-get-request-with-parameters/#findComment-1594360 Share on other sites More sharing options...
jaybo Posted March 10, 2022 Author Share Posted March 10, 2022 Thanks kicken, sorted it right out! Quote Link to comment https://forums.phpfreaks.com/topic/314592-php-curl-get-request-with-parameters/#findComment-1594362 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.