trollolodn

#php : call restful service php

Mar 26th, 2013
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.59 KB | None | 0 0
  1. /* Example of calling GET request */
  2.  
  3. //next example will recieve all messages for specific conversation
  4. $service_url = 'http://example.com/api/conversations/[CONV_CODE]/messages&apikey=[API_KEY]';
  5. $curl = curl_init($service_url);
  6. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  7. $curl_response = curl_exec($curl);
  8. if ($curl_response === false) {
  9.     $info = curl_getinfo($curl);
  10.     curl_close($curl);
  11.     die('error occured during curl exec. Additioanl info: ' . var_export($info));
  12. }
  13. curl_close($curl);
  14. $decoded = json_decode($curl_response);
  15. if (isset($decoded->response->status) && $decoded->response->status == 'ERROR') {
  16.     die('error occured: ' . $decoded->response->errormessage);
  17. }
  18. echo 'response ok!';
  19. var_export($decoded->response);
  20.  
  21.  
  22. /* Example of calling POST request */
  23.  
  24. //next example will insert new conversation
  25. $service_url = 'http://example.com/api/conversations';
  26. $curl = curl_init($service_url);
  27. $curl_post_data = array(
  28.         'message' => 'test message',
  29.         'useridentifier' => '[email protected]',
  30.         'department' => 'departmentId001',
  31.         'subject' => 'My first conversation',
  32.         'recipient' => '[email protected]',
  33.         'apikey' => 'key001'
  34. );
  35. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  36. curl_setopt($curl, CURLOPT_POST, true);
  37. curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_post_data);
  38. $curl_response = curl_exec($curl);
  39. if ($curl_response === false) {
  40.     $info = curl_getinfo($curl);
  41.     curl_close($curl);
  42.     die('error occured during curl exec. Additioanl info: ' . var_export($info));
  43. }
  44. curl_close($curl);
  45. $decoded = json_decode($curl_response);
  46. if (isset($decoded->response->status) && $decoded->response->status == 'ERROR') {
  47.     die('error occured: ' . $decoded->response->errormessage);
  48. }
  49. echo 'response ok!';
  50. var_export($decoded->response);
  51.  
  52.  
  53. /* Example of calling PUT request */
  54.  
  55. //next eample will change status of specific conversation to resolve
  56. $service_url = 'http://example.com/api/conversations/cid123/status';
  57. $ch = curl_init($service_url);
  58.  
  59. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  60. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
  61. $data = array("status" => 'R');
  62. curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($data));
  63. $response = curl_exec($ch);
  64. if ($curl_response === false) {
  65.     $info = curl_getinfo($curl);
  66.     curl_close($curl);
  67.     die('error occured during curl exec. Additioanl info: ' . var_export($info));
  68. }
  69. curl_close($curl);
  70. $decoded = json_decode($curl_response);
  71. if (isset($decoded->response->status) && $decoded->response->status == 'ERROR') {
  72.     die('error occured: ' . $decoded->response->errormessage);
  73. }
  74. echo 'response ok!';
  75. var_export($decoded->response);
  76.  
  77.  
  78. /* Example of calling DELETE request */
  79.  
  80. $service_url = 'http://example.com/api/conversations/[CONVERSATION_ID]';
  81. $ch = curl_init($service_url);
  82. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  83. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
  84. $curl_post_data = array(
  85.         'note' => 'this is spam!',
  86.         'useridentifier' => '[email protected]',
  87.         'apikey' => 'key001'
  88. );
  89. curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_post_data);
  90. $response = curl_exec($ch);
  91. if ($curl_response === false) {
  92.     $info = curl_getinfo($curl);
  93.     curl_close($curl);
  94.     die('error occured during curl exec. Additioanl info: ' . var_export($info));
  95. }
  96. curl_close($curl);
  97. $decoded = json_decode($curl_response);
  98. if (isset($decoded->response->status) && $decoded->response->status == 'ERROR') {
  99.     die('error occured: ' . $decoded->response->errormessage);
  100. }
  101. echo 'response ok!';
  102. var_export($decoded->response);
Advertisement
Add Comment
Please, Sign In to add comment