How to translate this command line in php curl?

I have a command line bit that I want to translate to php. I'm afraid.

Here is a line of code

$ curl -H "Authorization: 622cee5f8c99c81e87614e9efc63eddb" https://api.service.com/member

the big line will be the variable I would go into.

What does it look like in PHP?

+3
source share
4 answers

First you need to analyze what this line does:

$ curl -H "Authorization: 622cee5f8c99c81e87614e9efc63eddb" https://api.service.com/member

It is not complicated, you will find all the switches explained on the curl manpage :

-H, --header <header>: (HTTP) An optional header to use when retrieving a web page. You can specify any number of additional headers. [...]

You can add a header through curl_setopt_array Docs in PHP (all available parameters are explained in the curl_setopt Docs ):

$ch = curl_init('https://api.service.com/member');
// set URL and other appropriate options
$options = array(        
    CURLOPT_HEADER => false,
    CURLOPT_HTTPHEADER => array("Authorization: 622cee5f8c99c81e87614e9efc63eddb"),
);
curl_setopt_array($ch, $options);
curl_exec($ch); // grab URL and pass it to the browser
curl_close($ch);

curl PHP-HTTP-, , ( , ):

$options = array('http' => array(
    'header' => array("Authorization: 622cee5f8c99c81e87614e9efc63eddb"),
));
$context = stream_context_create($options);
$result = file_get_contents('https://api.service.com/member', 0, $context);
+3

curl_* php. curl_setopt() .

+1

1) Curl

2) exec()

exec('curl -H "Authorization: 622cee5f8c99c81e87614e9efc63eddb" https://api.service.com/member');

3) file_get_contents(), ...

<?php
// Create a stream
$opts = array(
  'http'=>array(
    'method'=>"GET",
    'header'=>"Authorization: 622cee5f8c99c81e87614e9efc63eddb"
  )
);

$context = stream_context_create($opts);

// Open the file using the HTTP headers set above
$file = file_get_contents('https://api.service.com/member', false, $context);
?>
+1

, PHP cURL, curl_setopt() HTTP- :

<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.service.com/member");
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: 622cee5f8c99c81e87614e9efc63eddb")); 
curl_exec($ch);
0

All Articles