Advertisement
metalx1000

Basic PHP curl commands with JSON output

May 4th, 2016
696
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.25 KB | None | 0 0
  1. <?php
  2. header('Content-Type: application/json');
  3.  
  4. include("auth.php");
  5. $baseURL = "https://example.com/Application";
  6. $loginUrl = "$baseURL/Login/process";
  7. $start = $_GET['start'];
  8.  
  9. $reqURL = "$baseURL/Ajax/DayNotes/GetByDate/?start=$start";
  10. //init curl
  11. $ch = curl_init();
  12.  
  13. //Set the URL to work with
  14. curl_setopt($ch, CURLOPT_URL, $loginUrl);
  15.  
  16. // ENABLE HTTP POST
  17. curl_setopt($ch, CURLOPT_POST, 1);
  18.  
  19. //Set the post parameters
  20. curl_setopt($ch, CURLOPT_POSTFIELDS, 'username='.$username.'&password='.$password);
  21.  
  22. //Handle cookies for the login
  23. curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
  24.  
  25. //Setting CURLOPT_RETURNTRANSFER variable to 1 will force cURL
  26. //not to print out the results of its query.
  27. //Instead, it will return the results as a string return value
  28. //from curl_exec() instead of the usual true/false.
  29. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  30.  
  31. //execute the request (the login)
  32. $store = curl_exec($ch);
  33.  
  34. //the login is now done and you can continue to get the
  35. //protected content.
  36.  
  37. //set the URL to the protected file
  38. curl_setopt($ch, CURLOPT_URL, $reqURL);
  39.  
  40. //execute the request
  41. $content = curl_exec($ch);
  42.  
  43. //save the data to disk
  44. //file_put_contents('test', $content);
  45. echo json_encode($content);
  46. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement