php怎么调用其他网站提供的api 接口

2025-05-08 21:50:04
推荐回答(4个)
回答1:

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

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

$url = 'http://localhost/openUser.php?act=get_user_list&type=json';
$ch = acurl_init ();
acurl_setopt ( $ch, CURLOPT_URL, $url );
acurl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
acurl_setopt ( $ch, CURLOPT_CONNECTTIMEOUT, 10 );
acurl_setopt ( $ch, CURLOPT_POST, 1 ); //启用POST提交
$file_contents = curl_exec ( $ch );

回答2:

http://ericxu131.iteye.com/blog/363664 看一下这个文章,php模拟post请求,然后其他网站api给你返回数据,你根据数据判断执行结果就行了!

回答3:

通过php模拟post请求即可调用。

php 模拟POST提交的方法:

通过curl函数 

Php代码:

  1. $post_data = array(); 

  2. $post_data['clientname'] = "test08"; 

  3. $post_data['clientpasswd'] = "test08"; 

  4. $post_data['submit'] = "submit"; 

  5. $url='http://xxx.xxx.xxx.xx/xx/xxx/top.php'; 

  6. $o=""; 

  7. foreach ($post_data as $k=>$v) 

  8. $o.= "$k=".urlencode($v)."&"; 

  9. $post_data=substr($o,0,-1); 

  10. $ch = curl_init(); 

  11. curl_setopt($ch, CURLOPT_POST, 1); 

  12. curl_setopt($ch, CURLOPT_HEADER, 0); 

  13. curl_setopt($ch, CURLOPT_URL,$url); 

  14. //为了支持cookie 

  15. curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt'); 

  16. curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); 

  17. $result = curl_exec($ch); 

回答4:

你这个问题不够详细啊!