如何使用cURL格式化已解析的JSON数据

时间:2019-01-11 03:34:47

标签: php curl

免责声明 这不是在密码保护的专用apache服务器上的实时产品。我知道我的代码容易被注入等。

我有一个php文件(如下),该文件连接到Web服务并返回json数据。

它可以工作并且数据返回,但用户无法读取。我想知道如何格式化数据,我正在尝试使用json_decode,但无法弄清楚。这是代码;

<!DOCTYPE html>
<html>
<head>
<title>Points of Interest</title>
<link rel="stylesheet" type="text/css" href="custom.css">
<link href="https://fonts.googleapis.com/css?family=Raleway" rel="stylesheet">
<link rel="stylesheet" type="text/css" href="skeleton.css">

</head>
<body>
  <img src="heroxd.jpg" alt="">

<div class="container">
  <div class="twelve-columns">
        <h1>Welcome to Visit Hampshire!</h1>
        <p>Linking users to interesting areas in Hampshire for 25 years</p>
        <p>Please select a type below</p>
        <form method="get">
        <input type="text" id="type" class="margin-top-30" value="Enter Type" name="type" />
        <input type="submit" id="submit" />
        </form>
  </div>
</div>
<?php
//$a = $_GET["region"];
$b = $_GET["type"];

// Initialise the cURL connection
$connection = curl_init();

// Specify the URL to connect to - this can be PHP, HTML or anything else!
curl_setopt($connection, CURLOPT_URL, "****/return_poi2.php?region=Hampshire&type=$b");


// This option ensures that the HTTP response is *returned* from curl_exec(),
// (see below) rather than being output to screen.
curl_setopt($connection,CURLOPT_RETURNTRANSFER,1);

// Do not include the HTTP header in the response.
curl_setopt($connection,CURLOPT_HEADER, 0);

// Actually connect to the remote URL. The response is
// returned from curl_exec() and placed in $response.
$response = curl_exec($connection);

// Close the connection.
curl_close($connection);

// Echo the response back from the server (for illustration purposes)
echo $response;
//$data = json_decode($response, true);

 ?>


</body>
</html>

这就是返回的数据的样子;

enter image description here

0 个答案:

没有答案