PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/democracyinaction/client.php

https://github.com/radicaldesigns/jaguar
PHP | 57 lines | 48 code | 9 blank | 0 comment | 8 complexity | 16ca466567885898c78be1bb076199df MD5 | raw file
Possible License(s): MIT, LGPL-2.1
  1. <?php
  2. class DemocracyInAction_Client {
  3. var $cookiefile;
  4. function __construct($cookiefile=null) {
  5. if(is_null($cookiefile)) {
  6. $cookiefile = tempnam(null, 'DemocracyInAction_Client');
  7. }
  8. $this->cookiefile = $cookiefile;
  9. }
  10. function send($url, $fields, $method=null) {
  11. $curl_handle = curl_init();
  12. curl_setopt($curl_handle, CURLOPT_SSL_VERIFYPEER, false);
  13. curl_setopt($curl_handle, CURLOPT_HEADER, 0);
  14. curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
  15. curl_setopt($curl_handle, CURLOPT_COOKIEFILE, $this->cookiefile);
  16. curl_setopt($curl_handle, CURLOPT_COOKIEJAR, $this->cookiefile);
  17. if(is_null($method)) {
  18. curl_setopt($curl_handle, CURLOPT_HTTPGET, 1);
  19. } elseif('post' == $method) {
  20. curl_setopt($curl_handle, CURLOPT_POST, 1);
  21. } else {
  22. trigger_error('method not supported');
  23. }
  24. curl_setopt($curl_handle, CURLOPT_URL, $url);
  25. if(is_array($fields)) {
  26. $ret = array();
  27. foreach($fields as $key => $value) {
  28. if( is_array($value)){
  29. foreach($value as $condition) $ret[] = "$key=".urlencode($condition);
  30. } else {
  31. $ret[] = "$key=".urlencode($value);
  32. }
  33. }
  34. $fields = implode('&', $ret);
  35. }
  36. curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $fields);
  37. if( defined( 'DIA_DEBUG')) {
  38. print 'DIA CALL: ' . $url . "\n";
  39. var_dump($fields);
  40. print "\n";
  41. }
  42. $data = curl_exec($curl_handle);
  43. $status = curl_getinfo($curl_handle, CURLINFO_HTTP_CODE);
  44. curl_close($curl_handle);
  45. return array($status, $data);
  46. }
  47. }
  48. ?>