Simple PHP Rest Cient using curl


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. <?php
  2. $url = 'https://www.google.com';
  3. $method = 'POST';
  4.  
  5. # headers and data (this is API dependent, some uses XML)
  6. $headers = array(
  7. 'Accept: application/json',
  8. 'Content-Type: application/json',
  9. );
  10. 'firstName'=> 'John',
  11. 'lastName'=> 'Doe'
  12. ));
  13.  
  14. $handle = curl_init();
  15. curl_setopt($handle, CURLOPT_URL, $url);
  16. curl_setopt($handle, CURLOPT_HTTPHEADER, $headers);
  17. curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
  18. curl_setopt($handle, CURLOPT_SSL_VERIFYHOST, false);
  19. curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false);
  20.  
  21. switch($method) {
  22. case 'GET':
  23. break;
  24. case 'POST':
  25. curl_setopt($handle, CURLOPT_POST, true);
  26. curl_setopt($handle, CURLOPT_POSTFIELDS, $data);
  27. break;
  28. case 'PUT':
  29. curl_setopt($handle, CURLOPT_CUSTOMREQUEST, 'PUT');
  30. curl_setopt($handle, CURLOPT_POSTFIELDS, $data);
  31. break;
  32. case 'DELETE':
  33. curl_setopt($handle, CURLOPT_CUSTOMREQUEST, 'DELETE');
  34. break;
  35. }
  36.  
  37. $response = curl_exec($handle);
  38. $code = curl_getinfo($handle, CURLINFO_HTTP_CODE);

URL: http://singletonio.blogspot.com/2009/07/simple-php-rest-client-using-curl.html

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.