PHP – CURL Authentication

MacBook avec code sur un bureau

To authenticate a cURL request in PHP, the option to know is CURLOPT_USERPWD. It sends a username / password pair in the user:password format. Basic authentication $ch = curl_init(‘https://example.com/api’); curl_setopt($ch, CURLOPT_USERPWD, ‘user:password’); curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); $response = curl_exec($ch); if ($response === false) { throw new RuntimeException(curl_error($ch)); } curl_close($ch); Setting … Read more

Categories PHP