PHP中调用接口
如:http://localhost/operate.php?act=get_user_list&type=json

在这里operate.php相当于一个接口,其中get_user_list 是一个API(获取用户列表),讲求返回的数据类型为JSON格式。

你只需要在你PHP代码中执行这条链接他就会返回。
GET方式的直接使用
$file_contents = file_get_content(‘http://localhost/operate.php?act=get_user_list&type=json’)

POST方式得用下面的(需要开启PHP curl支持)。

  1. $url = 'http://localhost/operate.php?act=get_user_list&type=json';
  2. $ch = curl_init ();
  3. curl_setopt ( $ch, CURLOPT_URL, $url );
  4. curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
  5. curl_setopt ( $ch, CURLOPT_CONNECTTIMEOUT, 10 );
  6. curl_setopt ( $ch, CURLOPT_POST, 1 ); //启用POST提交
  7. $file_contents = curl_exec ( $ch );
  8. curl_close ( $ch );

post数据的三种方法

  1. <?php
  2. // PHP POST数据的三种方法
  3. // php有三种方法可以post数据,分别为Curl、socket、file_get_contents:
  4. /**
  5. * Socket版本
  6. * 使用方法:
  7. * $post_string = "app=socket&version=beta";
  8. * request_by_socket('facebook.cn','/restServer.php',$post_string);
  9. */
  10. function request_by_socket($remote_server, $remote_path, $post_string, $port = 80, $timeout = 30)
  11. {
  12. $socket = fsockopen($remote_server, $port, $errno, $errstr, $timeout);
  13. if (!$socket) die("$errstr($errno)");
  14. fwrite($socket, "POST $remote_path HTTP/1.0\r\n");
  15. fwrite($socket, "User-Agent: Socket Example\r\n");
  16. fwrite($socket, "HOST: $remote_server\r\n");
  17. fwrite($socket, "Content-type: application/x-www-form-urlencoded\r\n");
  18. fwrite($socket, "Content-length: " . (strlen($post_string) + 8) . '\r\n');
  19. fwrite($socket, "Accept:*/*\r\n");
  20. fwrite($socket, "\r\n");
  21. fwrite($socket, "mypost=$post_string\r\n");
  22. fwrite($socket, "\r\n");
  23. $header = "";
  24. while ($str = trim(fgets($socket, 4096))) {
  25. $header .= $str;
  26. }
  27. $data = "";
  28. while (!feof($socket)) {
  29. $data .= fgets($socket, 4096);
  30. }
  31. return $data;
  32. }
  33. /**
  34. * Curl版本
  35. * 使用方法:
  36. * $post_string = "app=request&version=beta";
  37. * request_by_curl('http://facebook.cn/restServer.php',$post_string);
  38. */
  39. function request_by_curl($remote_server, $post_string)
  40. {
  41. $ch = curl_init();
  42. curl_setopt($ch, CURLOPT_URL, $remote_server);
  43. curl_setopt($ch, CURLOPT_POSTFIELDS, 'mypost=' . $post_string);
  44. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  45. curl_setopt($ch, CURLOPT_USERAGENT, "Jimmy's CURL Example beta");
  46. $data = curl_exec($ch);
  47. curl_close($ch);
  48. return $data;
  49. }
  50. /**
  51. * 其它版本
  52. * 使用方法:
  53. * $post_string = "app=request&version=beta";
  54. * request_by_other('http://facebook.cn/restServer.php',$post_string);
  55. */
  56. function request_by_other($remote_server, $post_string)
  57. {
  58. $context = array(
  59. 'http' => array(
  60. 'method' => 'POST',
  61. 'header' => 'Content-type: application/x-www-form-urlencoded' .
  62. '\r\n'.'User-Agent : Jimmy\'s POST Example beta' .
  63. '\r\n'.'Content-length:' . strlen($post_string) + 8,
  64. 'content' => 'mypost=' . $post_string)
  65. );
  66. $stream_context = stream_context_create($context);
  67. $data = file_get_contents($remote_server, false, $stream_context);
  68. return $data;
  69. }

分类: PHP

标签:   API