Jump to content

PHP curl GET request with Parameters


jaybo
Go to solution Solved by kicken,

Recommended Posts

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;
}
?>

 

Link to comment
Share on other sites

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 by ginerjm
Link to comment
Share on other sites

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)

Link to comment
Share on other sites

  • Solution
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);

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.