fork download
  1. <?php
  2. class PayTrace_Json_API {
  3.  
  4. protected $credentials;
  5.  
  6. public function __construct( $client_username = PAYTRACE_USERNAME, $client_password = PAYTRACE_PASSWORD, $client_integrator_id = PAYTRACE_INTEGRATORID, $client_url = PAYTRACE_JSON_URL ) {
  7. $this->credentials = (object) array(
  8. 'url' => $client_url . '/v3',
  9. 'client_username' => $client_username,
  10. 'client_password' => $client_password,
  11. 'client_integrator_id' => $client_integrator_id
  12. );
  13. }
  14.  
  15. public function get_user( $customer_id ){
  16. return $this->send(array(), $this->credentials->url . '/customer/' . $customer_id, $this->headers(), 'GET');
  17. }
  18.  
  19.  
  20. public function token(){
  21. $parameters = http_build_query(array(
  22. 'grant_type' => 'client_credentials',
  23. 'client_id' => $this->credentials->client_username,
  24. 'client_secret' => $this->credentials->client_password,
  25. ));
  26.  
  27. $headers = array(
  28. 'Content-Type: application/x-www-form-urlencoded'
  29. );
  30.  
  31. return $this->send($parameters, $this->credentials->url . '/token', $headers, 'POST');
  32. }
  33.  
  34. public function headers(){
  35. $token = $this->token();
  36. $bearer = $token['data']['access_token'];
  37. $headers = array(
  38. 'Content-type: application/json',
  39. 'Cache-Control: no-cache',
  40. 'Authorization: ' . sprintf('Bearer %s', $bearer ),
  41. 'X-Integrator-Id: ' . $this->credentials->client_integrator_id
  42. );
  43.  
  44. return $headers;
  45. }
  46.  
  47. public function send( $parameters, $url, $headers, $method ){
  48.  
  49. $ch = curl_init($url);
  50. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  51.  
  52. if( $method === 'POST' ){
  53. curl_setopt($ch, CURLOPT_POST, true);
  54. if( $parameters ){
  55. curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters);
  56. }
  57. } elseif( $method === 'GET' ){
  58. curl_setopt($ch, CURLOPT_HTTPGET, true);
  59. }
  60.  
  61. if( ! empty($headers) ){
  62. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  63. }
  64.  
  65. $response = curl_exec($ch);
  66. if( curl_errno($ch) ) {
  67. return ['error' => curl_error($ch)];
  68. }
  69. curl_close($ch);
  70.  
  71. return json_decode($response, true);
  72. }
  73. }
  74.  
  75. $paytrace_json_api = new PayTrace_Json_API('carl.fontanos2@gmail.com', '2R9J7YV2FMdc', '999cfontanos', 'https://a...content-available-to-author-only...e.com');
  76. echo '<pre>';
  77. print_r($paytrace_json_api->get_user(59485));
  78. exit();
Success #stdin #stdout #stderr 0.04s 26032KB
stdin
Standard input is empty
stdout
<pre>Array
(
    [error] => Could not resolve host: api.sandbox.paytrace.com
)
stderr
PHP Notice:  Undefined index: data in /home/lBeU5B/prog.php on line 36