Ovh.class.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. class Ovh{
  3. public $applicationKey,$applicationSecret,$consumerKey,$host = 'https://eu.api.ovh.com';
  4. public function consumerKey(){
  5. $body = '{
  6. "accessRules": [
  7. {
  8. "method": "GET",
  9. "path": "/*"
  10. },
  11. {
  12. "method": "POST",
  13. "path": "/*"
  14. }
  15. ],
  16. "redirection":"https://www.idleman.fr/"
  17. }';
  18. $headers = array();
  19. $headers['Accept'] = 'application/json';
  20. return $this->rest('POST','/1.0/auth/credential',$body,$headers);
  21. }
  22. public function domain(){
  23. return $this->rest('GET','/1.0/domain/zone/idleman.fr/record/5136453749','');
  24. }
  25. public function getDomain($zone = ''){
  26. if(!empty($zone)) $zone = '/'.$zone;
  27. return $this->rest('GET','/1.0/domain/zone'.$zone,'');
  28. }
  29. public function setDomain($zone,$subdomain,$target){
  30. $body = array(
  31. 'fieldType' => 'CNAME',
  32. 'subDomain' => $subdomain,
  33. 'target' => 'idleman.fr.',
  34. 'ttl' => '0'
  35. );
  36. $response = $this->rest('POST','/1.0/domain/zone/idleman.fr/record',json_encode($body));
  37. $this->rest('POST','/1.0/domain/zone/idleman.fr/refresh');
  38. return $response;
  39. }
  40. //Requete rest
  41. public function rest($method,$action,$body='',$headers = null){
  42. $url = $this->host.$action;
  43. $ch = curl_init();
  44. $options[CURLOPT_URL] = $url;
  45. $options[CURLOPT_RETURNTRANSFER] = true;
  46. $options[CURLOPT_SSL_VERIFYPEER] = false;
  47. $options[CURLOPT_FOLLOWLOCATION] = true;
  48. $options[CURLOPT_USERAGENT] = 'Awesome erp';
  49. $options[CURLOPT_HEADER] = true;
  50. $options[CURLOPT_CUSTOMREQUEST] = $method;
  51. if(!empty($body)) $options[CURLOPT_POSTFIELDS] = $body;
  52. if(empty($headers)) $headers = array();
  53. $options[CURLOPT_HTTPHEADER] = $headers;
  54. curl_setopt_array($ch,$options);
  55. $headers['Content-type'] = 'application/json; charset=utf-8';
  56. $headers['X-Ovh-Application']=$this->applicationKey;
  57. if($action!='/1.0/auth/credential'){
  58. $time = file_get_contents('https://eu.api.ovh.com/1.0/auth/time');
  59. $signature = $this->applicationSecret."+".$this->consumerKey."+".$method."+".$url."+".$body."+".$time;
  60. $signature = "$1$" . sha1($signature);
  61. $headers['X-Ovh-Signature'] = $signature;
  62. $headers['X-Ovh-Consumer'] = $this->consumerKey;
  63. $headers['X-Ovh-Timestamp'] = $time;
  64. }
  65. $headers['cache-control'] = 'no-cache';
  66. $cmd = "curl ".$url." ";
  67. foreach ($headers as $key => $value) {
  68. $cmd .= " -H '".$key.": ".$value."' ";
  69. }
  70. if(!empty($body)) $cmd .= " -d '".$body."' ";
  71. $response = exec($cmd);
  72. if($response === false) throw new Exception(curl_error($ch));
  73. curl_close($ch);
  74. return json_decode($response,true);
  75. }
  76. }
  77. ?>