我必须做一个网络服务。于是引用了网上的一些教程,想出了下面的代码
索引.php
<html>
<head>
<title>Form page</title>
</head>
<body>
<form action="http://localhost:81/my%20web%20service/webservice" method="get">
Table name:<br>
<input type="text" name="s" value=""><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
网络服务.php
<?php
include('connectdb.php');
$something = $_POST['s'];
$sql = "SELECT * FROM $something";
$result = $conn->query($sql);
$myArray = array();
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$myArray[] = $row;
echo "name: " . $row["name"]. " - town: " . $row["town"]. " - telephone: " . $row["telephone"]. "<br>";
}
//$data = json_encode($myArray);
//var_dump($data);
$url = 'http://localhost:81/my%20web%20service/show%details.php';
$ch=curl_init($url);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
$string = http_build_query($myArray);
curl_setopt($ch, CURLOPT_POSTFIELDS, $string);
$applist = curl_exec($ch);
curl_close($ch);
} else {
echo "0 results";
}
//$result->close();
$conn->close();
?>
connectdb.php
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "webservice_trial";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} else{
echo "connection successful.<br>";
}
?>
现在我要做的是让数据显示在另一个页面上。 即我想将 webservice.php 中的数据从 json 格式传输到另一个页面(显示 details.php)。
显示细节.php
<html>
<head>
<title>Show details page</title>
</head>
<body>
<?php
print_r($_POST['data']);
?>
</body>
</html>
我如何将 json 字符串从 Web 服务重定向到显示 details.php
我得到的是这个
connection successful.
name: hilton - town: colombo - telephone: 774933705
name: galadari - town: colombo - telephone: 112894143
name: mt. lavinia - town: mt. lavinia - telephone: 773580324
string(257) "[{"name":"hilton","town":"colombo","telephone":"774933705","description":"excellent"},{"name":"galadari","town":"colombo","telephone":"112894143","description":"best"},{"name":"mt. lavinia","town":"mt. lavinia","telephone":"773580324","description":"good"}]"
Access forbidden!
You don't have permission to access the requested object. It is either read- protected or not readable by the server.
If you think this is a server error, please contact the webmaster.
Error 403
最佳答案
尝试使用下面的代码,将其添加到您的虚拟主机配置中
<Directory "E:/MyProjects/projectname/">
Options Indexes FollowSymLinks MultiViews
AllowOverride all
Order Deny,Allow
Allow from all
Require all granted
</Directory>
还有
Options FollowSymLinks
到:
Options +FollowSymLinks
我用这种方法解决了同样的问题。希望对您有所帮助。
关于php - 使用 json 和 cURL 访问禁止的错误 403 Web 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35545870/