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

2025-05-06 10:59:05
推荐回答(4个)
回答1:

两种比较简单的方法:

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 也是可以的

回答2:

可以使用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);

回答3:

curl

回答4:

不会