如何用php调用外部接口json数据

如题所述

两种比较简单的方法:

1、使用curl 

$url = "http://www.xxxxxxxxxx.com/";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT , 30);
$output = curl_exec($ch);
curl_close($ch);

echo $output;

2、使用file_get_contents

$output = file_get_contents($url);
echo $output;


3 、使用socket 也是可以的

温馨提示:答案为网友推荐,仅供参考
第1个回答  2017-07-25

可以使用php+crul模拟请求接口,如curl模拟post请求

   $url = "http://localhost/web_services.php";
  $post_data = array ("username" => "bob","key" => "12345");
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  // post数据
  curl_setopt($ch, CURLOPT_POST, 1);
  // post的变量
  curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
  $output = curl_exec($ch);
  curl_close($ch);
  //打印获得的数据
  print_r($output);

第2个回答  2017-07-25
curl
第3个回答  2017-07-25
不会
第4个回答  2017-07-25
不会