7

I want perform a CURL request with parameters and values by using GET method but I don't want to mix them before passing to curl like it is in the string:

www.example.com/index.php?parameter=value

I would like to pass separate url string and query string or at best url string + an array of parameters and values to CURL with letting to know CURL that I want to use GET method (CURLOPT_HTTPGET=TRUE).

Is there any CURLOPT_POSTFIELDS equivalent for GET method?

Daniel Widdis
  • 8,424
  • 13
  • 41
  • 63
Jimmix
  • 5,644
  • 6
  • 44
  • 71

1 Answers1

8

Use the function http_build_query() to create the query string from an associative array.

$query = http_build_query($params);
curl_setopt($ch, CURLOPT_URL, "www.example.com/index.php?$query");
Daniel Widdis
  • 8,424
  • 13
  • 41
  • 63
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • so there is no field option method like in POST method isn't it? just to separate url and parameters with their values? it makes a bit mess in debugging that joining www.url-to-fetch.com/index.php?$query because then it leads to www.url-to-fetch.com/index.php?parameter=value but the actual url is www.url-to-fetch.com/index.php and second (different) thing are all parameters and their values. – Jimmix May 06 '13 at 18:00
  • 1
    The whole difference between `GET` and `POST` is that `GET` puts the parameters in the URL, `POST` puts it in the data. – Barmar May 06 '13 at 18:07
  • I hoped that maybe curl could manage that in different way and build www.url-to-fetch.com/index.php?parameter=value just before requesting page and give an access to url and parameters as separate fields but if not then i will manage that on my own. thanks for clarification, – Jimmix May 06 '13 at 18:18