Guest User

Untitled

a guest
Jan 21st, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.62 KB | None | 0 0
  1. <?php
  2. function call($vars){
  3. //* generic Curl method to call the API server
  4. //* gets cookie from client, sets it as a temp file for the curl cookie jar
  5. //* makes request on behalf of the remote client and then saves the cookie back to the client
  6.  
  7. //open temp file
  8. $tmpFileName = 'cookie_'.(microtime(true)*100).rand(100,999);
  9. $tmpFileHandle = fopen($this->tempDir.$tmpFileName, 'w') or die("can't open cookie cache file");
  10. fwrite($tmpFileHandle, base64_decode($_COOKIE['remoteID']));
  11. fclose($tmpFileHandle);
  12.  
  13. //build var string
  14. $first = true;
  15. foreach($vars as $varName => $var){
  16. if($first){
  17. $getVars .= "?";
  18. }else{
  19. $getVars .= '&';
  20. }
  21. $getVars .= urlencode($varName).'='.urlencode($var);
  22. $first = false;
  23. }
  24.  
  25. //call remote server
  26. $ch = curl_init();
  27. curl_setopt($ch, CURLOPT_URL, $this->apiServerURL."/".$this->params['section']."/".$this->params['method'].$getVars);
  28. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  29. curl_setopt($ch, CURLOPT_COOKIEJAR, $this->tempDir.$tmpFileName);
  30. curl_setopt($ch, CURLOPT_COOKIEFILE, $this->tempDir.$tmpFileName);
  31. $output = json_decode(curl_exec($ch));
  32. $info = curl_getinfo($ch);
  33. curl_close($ch);
  34. unset($ch); // run in to a problem with this a few times
  35.  
  36. //set cookie on client
  37. $tmpFileHandle = fopen($this->tempDir.$tmpFileName, 'rb') or die("can't open cookie cache file");
  38. $_COOKIE['remoteID'] = base64_encode(fread($tmpFileHandle, filesize($this->tempDir.$tmpFileName)));
  39. fclose($tmpFileHandle);
  40.  
  41. return $output;
  42. }
Add Comment
Please, Sign In to add comment