php - 如何在 PHP 中使用 file_get_contents 修复 404 错误?

标签 php mysql apache .htaccess

当我从 htdocs 的子文件夹 运行以下 php 文件时,出现 404 错误:“在此服务器上找不到请求的 URL。”

我们将子文件夹称为“其他”。

当从 htdocs 文件夹运行相同的文件时,我收到错误:“ fatal error :在 C:\xampp\htdocs\other\DBConnection.php 中找不到类 'mysqli' on line 12 PHP。”

我希望能够从“其他”文件夹成功运行该文件,因为我将使用它来测试多个项目和网站。

我认为问题在于 file_get_contents("http://www.omdbapi.com/?t=Open+Water&y=&plot=full&r=json "); 中缺少某些内容线。 IE。告诉此命令在 htdocs 文件夹中查找它将能够访问互联网或授权它从“其他”文件访问互联网。

我已经在这里和网络上进行了研究,但找不到答案。我曾尝试将连接代码移动到“OMDB 查询”文件中,但这会产生类似于上面第二个错误的错误。谁能帮我解决这个问题?

Windows 10 网 bean 8.0.2 XAMPP PHP 5.6.8 MySQL 5.6 访问 OMDb.com api。

代码:

<?php
/*
 * File: OMDB query.php
 * Author:  Jim Stevens
 * Date: 08/14/2015
 * Description:  Imports records from OMDb and inserts results into local mySQL DB.
 */
require_once 'C:\xampp\htdocs\other\DBConnection.php';
$json = file_get_contents("http://www.omdbapi.com/?t=Open+Water&y=&plot=full&r=json");

// Decodes .json file, creates & populates an associative array.
$data = json_decode($json, TRUE);

// Extracts the array data.
$title = $data['Title'];
$year = $data['Year'];
$rated = $data['Rated'];
...more fields...

// Inserts extracted values into mySQL table 'myDB'.
$sql = "INSERT INTO movies.myDB(title, yr, rated, ...more fields...')";

if ($connection->query($sql) === TRUE) {
echo "New record created successfully";
}
...
?>

DBConnection.php 文件存储在“其他”文件夹中。它成功连接到“myDB”并按预期工作:

代码:

<?php

/*
 * File: DBConnection.php
 * Author:  Jim Stevens
 * Date: 08/09/2015
 * Description:  Creates and manages myDB database connections.
 */

require_once 'config.php'; // The file with username, password, dbname, & dbhostname.

$connection = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
if ($connection->connect_error) {
    die($connection->connect_error);
}

//  The following works correctly but is a leftover from initial testing and will be removed eventually.
$query = "SELECT * FROM myDB";
$result = $connection->query($query);
if (!$result) {
    die($connection->error);
}
$rows = $result->num_rows;
for ($j=0; $j < $rows; ++$j) {
    $result->data_seek($j);
    $row = $result->fetch_array(MYSQLI_ASSOC);

    echo 'Title: ' . $row['title'] . '<br>';
    echo 'Year: '  . $row['yr']    . '<br>';
    echo 'Rated: ' . $row['rated'] . '<br>';
    echo 'Actors: '. $row['actors']. '<br>';
    echo 'Plot: '  . $row['plot']  . '<br><br>';
}
$result->close();
$connection->close();
?>

最佳答案

您的 file_get_contents() 函数没有任何问题。

在解释器接触到这一行之前,你就收到了一个错误。

正如您的错误输出所说(您真的应该相信它),错误位于 DBConnection.php 文件的第 12 行,因此:

$connection = new mysqli($dbhost, $dbuser, $dbpass, $dbname);

                  ^ here

您可能没有安装 Mysqli。查看 how to install it 上的文档在 Windows 上,或查看 XAMPP 的文档。

关于php - 如何在 PHP 中使用 file_get_contents 修复 404 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32020600/

相关文章:

apache - apache中的这个配置是什么意思?

php - 创建随机字符串,检查数据库,如果发现再次运行 - 代码未按我的预期运行?

apache - 附加查询字符串的重写没有触发?

php - 根据 WooCommerce 中的自定义字段过滤产品

php - 来自 php MySQL 查询的 JSON 显示不存在的列

java - 如何从 Java 使用 MySQL 转义字符串文字中的单引号

mysql - 在 Mysql 中获取那些 id 不同的行

apache - 修复 .svn 目录权限

php - WooCommerce - 添加到购物车没有变体 ID

php - 为大型公司在 phpmyadmin 中创建大型 Web 应用程序