-2

I have to do a curl request to an API using PHP for my website.

My curl request =

curl -H "Content-Type: application/json" -H "Accept: application/json" -X GET -basic -u app:app "http://thewebsite.com"

My question is : How to translate -H options of this request in curl_exec() function in PHP ?

I have seen differents options like 'curl_setopt' but I don't know what is the correct option to use.

So to recap, which option for "Content-Type: application/json Accept: application/json" and for "-basic -u app:app" ?

Thanks :)

Flo

Florian B
  • 3
  • 6
  • 2
    Possible duplicate of [How do I make a request using HTTP basic authentication with PHP curl?](https://stackoverflow.com/questions/2140419/how-do-i-make-a-request-using-http-basic-authentication-with-php-curl) – Patrick Q Nov 06 '17 at 13:45

1 Answers1

2

The -H in curl indicates the headers of the request, so your curl will translate to php like:

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "http://thewebsite.com");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPGET, 1);

curl_setopt($ch, CURLOPT_USERPWD, "app:app");

$headers = 
    [
        "Content-Type: application/json",
        "Accept: application/json"
    ];
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);

curl_close($ch);
mega6382
  • 9,211
  • 17
  • 48
  • 69