M
M
Maxim Ivanov2015-08-05 02:08:28
PHP
Maxim Ivanov, 2015-08-05 02:08:28

How to run curl commands in PHP?

There are teams

Basic Authentication
$ curl -u "username" https://api.github.com

OAuth2 Token (sent in a header)
$ curl -H "Authorization: token OAUTH-TOKEN" https://api.github.com

OAuth2 Token (sent as a parameter)
$ curl https://api.github.com/?access_token=OAUTH-TOKEN

But they all run only in the console, but how should this be written in PHP?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anton Tikhomirov, 2015-08-13
@omaxphp

There are built-in commands, do not fence bicycles.

$ch = curl_init ();

curl_setopt ($ch, CURLOPT_URL, 'https://api.github.com');
curl_setopt ($ch, CURLOPT_USERAGENT, 'cURL/php');
curl_setopt ($ch, CURLOPT_USERPWD, 'login:password');
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);

echo curl_exec ($ch);

And so, colleagues threw links above. Here is a detailed manual on options: php.net/manual/en/function.curl-setopt.php. whatever you want)

V
Vladimir Chernyshev, 2015-08-13
@VolCh

Alternatively shell_exec('curl -u "username" https://api.github.com ') or exec('curl -u "username" https://api.github.com ')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question