Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [PHP]API Inpost Tracking pomoc we wdrożeniu, api inpost
casperii
post
Post #1





Grupa: Zarejestrowani
Postów: 681
Pomógł: 28
Dołączył: 14.08.2014

Ostrzeżenie: (0%)
-----


Panowie próbuje zrozumieć dokumentacje API Inpost. Z tego co rozumiem, nie jest wymagane żadne tokeny, hasła, klucze etc. aby sprawdzać tracking przesyłki.
https://docs.inpost24.com/display/PL/%5B1.2...aprzesy%C5%82ki

<?php
require_once 'vendor/restApi.php';
$restApi = new RestApi(array(

));

print_r($restApi->getResponse());
print_r(json_decode($restApi->getResponse()));

?>


Poniżej restApi.php

  1. <?php
  2. class RestApi
  3. {
  4. protected $response;
  5. protected $info;
  6. protected $errno;
  7. protected $error;
  8. public function __construct($params)
  9. {
  10. $this->response = $this->curlInit(array(
  11. 'token' => $params['token'],
  12. 'url' => $params['url'],
  13. 'methodType' => $params['methodType'],
  14. 'params' => $params['params']
  15. ));
  16. }
  17. private function curlInit($params = array())
  18. {
  19. $ch = curl_init();
  20. switch($params['methodType']){
  21. case 'GET':
  22. curl_setopt($ch, CURLOPT_HTTPHEADER, array('HTTP/1.1: GET') );
  23. $getParams = null;
  24. if(!empty($params['params'])){
  25. foreach($params['params'] as $field_name => $field_value){
  26. $getParams .= $field_name.'='.urlencode($field_value).'&';
  27. }
  28. curl_setopt($ch, CURLOPT_URL, $params['url'].'?'.$getParams);
  29. }else{
  30. curl_setopt($ch, CURLOPT_URL, $params['url'].'?'.$params['token']);
  31. }
  32. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
  33. break;
  34. case 'POST':
  35. $string = json_encode($params['params']);
  36. #$string = $params['params'];
  37. curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-HTTP-Method-Override: POST') );
  38. curl_setopt($ch, CURLOPT_URL, $params['url'].'?token='.$params['token']);
  39. curl_setopt($ch, CURLOPT_POST, true);
  40. curl_setopt($ch, CURLOPT_POSTFIELDS, $string);
  41. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
  42. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  43. 'Content-Type: application/json',
  44. 'Content-Length: ' . strlen($string))
  45. );
  46. break;
  47. case 'PUT':
  48. $string = json_encode($params['params']);
  49. #$string = $params['params'];
  50. curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-HTTP-Method-Override: PUT') );
  51. curl_setopt($ch, CURLOPT_URL, $params['url'].'?token='.$params['token']);
  52. curl_setopt($ch, CURLOPT_POST, true);
  53. curl_setopt($ch, CURLOPT_POSTFIELDS, $string);
  54. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
  55. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  56. 'Content-Type: application/json',
  57. 'Content-Length: ' . strlen($string))
  58. );
  59. break;
  60. }
  61. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  62. $result = curl_exec($ch);
  63. $this->info = curl_getinfo($ch);
  64. $this->errno = curl_errno($ch);
  65. $this->error = curl_error($ch);
  66. if ($this->info['http_code'] != 200) {
  67.  
  68.  
  69. $response = json_encode(array(
  70. 'message' => 'Request failed',
  71. 'type' => 'RequestErrors',
  72. 'code_errors' => $this->config['errorCodes'][$info['http_code']]
  73. ));
  74.  
  75.  
  76. }
  77. return $result;
  78. }
  79. public function getResponse()
  80. {
  81. return $this->response;
  82. }
  83. public function getInfo()
  84. {
  85. return $this->info;
  86. }
  87. public function getErrno()
  88. {
  89. return $this->errno;
  90. }
  91. public function getError()
  92. {
  93. return $this->error;
  94. }
  95. }
  96. ?>


Nikt nie wie ? (IMG:style_emoticons/default/sad.gif)
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 24.08.2025 - 10:06