Skip to content

Instantly share code, notes, and snippets.

@AnrDaemon
Last active May 11, 2018 19:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AnrDaemon/94e2d8cd54d5ae90b41df25a4848f85e to your computer and use it in GitHub Desktop.
Save AnrDaemon/94e2d8cd54d5ae90b41df25a4848f85e to your computer and use it in GitHub Desktop.
Simple cURL setup with cURL internal session cookie handling
<?php
$url = new AnrDaemon\Net\Url("https://www.example.org/login/page");
// Ask for authentication password, fill the form
fwrite(STDERR, 'Enter password: ');
$form = [
"login" => 'admin',
"pass" => trim(fgets(STDIN)),
];
// Create cURL instance
$curl = new AnrDaemon\Net\Browser([
CURLOPT_CONNECTTIMEOUT_MS => 500,
]);
// Login to the website
$curl->post($url, $form);
$result = $curl->getinfo(CURLINFO_RESPONSE_CODE);
if($result != 303 && $result != 202)
die("Unauthorized.");
// Prepare endpoint URL
$url = $url->parse("/api/search");
$result = $curl->get($url->setParts(['query' => ['series' => $item['series']]]));
if($curl->getinfo(CURLINFO_RESPONSE_CODE) != 200)
throw new \Exception("Search failed");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment