OvhSms.class.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. class OvhSms{
  3. const ENDPOINT = 'https://eu.api.ovh.com/1.0';
  4. public $key,$secret,$consumer;
  5. function services(){
  6. return $this->rest(self::ENDPOINT.'/sms','GET');
  7. }
  8. //Infos sur le serrvice (credits restants etc...)
  9. /* eg service : sms-ry3016-1 */
  10. function infos($service){
  11. return $this->rest(self::ENDPOINT.'/sms/'.$service.'','GET');
  12. }
  13. //Sms reçu
  14. /* eg service : sms-ry3016-1 */
  15. function incoming($service){
  16. return $this->rest(self::ENDPOINT.'/sms/'.$service.'/incoming','GET');
  17. }
  18. //Sms en cours d'envois & etat
  19. /* eg service : sms-ry3016-1 */
  20. function pending($service){
  21. return $this->rest(self::ENDPOINT.'/sms/'.$service.'/jobs','GET');
  22. }
  23. //https://docs.ovh.com/fr/sms/envoyer_des_sms_avec_lapi_ovh_en_java/
  24. function send($service,$phone,$message){
  25. return $this->rest(self::ENDPOINT.'/sms/'.$service.'/jobs','POST',json_encode(array(
  26. 'message' => $message,
  27. 'charset' => 'UTF-8',
  28. 'senderForResponse' => true,
  29. 'receivers' => array($phone)
  30. )));
  31. }
  32. function rest($url,$method,$fields = array(),$headers = array()){
  33. $timestamp = file_get_contents(self::ENDPOINT.'/auth/time');
  34. $signature = $this->secret.'+'.$this->consumer.'+'.$method.'+'.$url.'++'.$timestamp;
  35. $signature = '$1$'.sha1($signature);
  36. $ch = curl_init();
  37. $curlOptions = array(
  38. CURLOPT_RETURNTRANSFER => 1,
  39. CURLOPT_URL => $url,
  40. CURLOPT_SSL_VERIFYPEER => false,
  41. CURLOPT_SSL_VERIFYHOST=> false,
  42. CURLOPT_USERAGENT => 'GuzzleHttp/6.5.3 curl/7.58.0 PHP/7.2.24-0ubuntu0.18.04.4',
  43. CURLOPT_SSLVERSION => 6,
  44. CURLOPT_FOLLOWLOCATION => true,
  45. CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  46. CURLOPT_HTTPHEADER => array_merge(array(
  47. 'Content-Type: application/json; charset=utf-8',
  48. 'X-Ovh-Application: '.$this->key,
  49. 'X-Ovh-Timestamp: '.$timestamp,
  50. 'X-Ovh-Signature: '.$signature,
  51. 'X-Ovh-Consumer: '.$this->consumer,
  52. ),$headers)
  53. );
  54. $curlOptions[CURLOPT_CUSTOMREQUEST] = $method;
  55. if(!empty($fields) || $method=='POST'){
  56. $curlOptions[CURLOPT_POST] = 1;
  57. $curlOptions[CURLOPT_POSTFIELDS] = $fields;
  58. }else{
  59. $curlOptions[CURLOPT_POST] = false;
  60. }
  61. $curlOptions[CURLINFO_HEADER_OUT] = true;
  62. curl_setopt_array($ch,$curlOptions);
  63. $result = curl_exec($ch);
  64. if($result === false) throw new Exception('Curl error : '.$result.curl_error($ch));
  65. return json_decode($result,true);
  66. curl_close ($ch);
  67. }
  68. }
  69. ?>