Advertisement
AnrDaemon

cURL sesion cookies example

Jan 30th, 2018
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.17 KB | None | 0 0
  1. <?php
  2.  
  3. $url = new AnrDaemon\Net\Url("http://test.example.org/login");
  4.  
  5. fwrite(STDERR, 'Enter password: ');
  6. $form = [
  7.   "login" => 'admin',
  8.   "password" => trim(fgets(STDIN)),
  9. ];
  10.  
  11. $curl = curl_init($url);
  12. curl_setopt_array($curl, [
  13.   CURLOPT_COOKIEFILE => '',
  14.   CURLOPT_COOKIESESSION => true,
  15.   CURLOPT_RETURNTRANSFER => true,
  16.   CURLOPT_CONNECTTIMEOUT_MS => 500,
  17.   //CURLOPT_URL => '',
  18.   //CURLOPT_HTTPGET => true,
  19.   CURLOPT_POSTFIELDS => $form,
  20. ]);
  21. curl_exec($curl);
  22. if(curl_getinfo($curl, CURLINFO_RESPONSE_CODE) != 303)
  23.   die("Unauthorized.");
  24.  
  25. $url = $url->parse("/backend/item?page=5&id=0");
  26.  
  27. try
  28. {
  29.   $form = [
  30.     "action" => "save",
  31.     "section_id" => 384,
  32.     "series" => 'series',
  33.     "name" => 'name',
  34.     "content" => 'content',
  35.   ];
  36.  
  37.   curl_setopt_array($curl, [
  38.     CURLOPT_URL => $url,
  39.     //CURLOPT_HTTPGET => true,
  40.     CURLOPT_POSTFIELDS => $form,
  41.   ]);
  42.   curl_exec($curl);
  43.   if(curl_getinfo($curl, CURLINFO_RESPONSE_CODE) != 303)
  44.     throw new \ErrorException("Send failed");
  45.  
  46.   print "Sent {$form['series']}.\n";
  47. }
  48. catch(\ErrorException $e)
  49. {
  50.   error_log($e);
  51.   print("Error posting {$form['series']} ({$e->getMessage()}).\n");
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement