2015-08-15 46 views
2

當我從htdocs的子文件夾運行以下php文件時,出現404錯誤:「在此服務器上找不到請求的URL」。如何在PHP中使用file_get_contents修復404錯誤?

讓我們打電話給子文件夾「其他」。

當從htdocs文件夾運行相同的文件時,出現錯誤:「致命錯誤:Class'mysqli'在第12行PHP的C:\ xampp \ htdocs \ other \ DBConnection.php中找不到。

我想能夠從「其他」文件夾成功運行文件,因爲我將用它來測試多個項目和網站。

我認爲問題在於file_get_contents(「http://www.omdbapi.com/?t=Open+Water&y=&plot=full&r=json」)中缺少某些內容。線。即。告訴該命令查看htdocs文件夾,在該文件夾中它將能夠訪問互聯網或授權它從「其他」文件訪問互聯網。

我在這裏和網上研究過,但找不到答案。我已經嘗試將連接代碼移入「OMDB查詢」文件,但會產生類似於上面第二個錯誤的錯誤。誰能幫我解決這個問題嗎?

的Windows 10 的NetBeans 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(); 
?> 

回答

1

沒有什麼不對您的file_get_contents()函數。

在翻譯員甚至觸及這一行之前,您會看到一個錯誤。

由於您的錯誤輸出說(你真的應該相信它),錯誤是位於您DBConnection.php文件的第12行,所以:

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

       ^here 

你可能沒有安裝的mysqli。查看Windows上how to install it的文檔,或查看XAMPP的文檔。

+0

拉斐爾,謝謝你的回覆。我將仔細檢查mysqli是否已正確安裝並正確安裝。當我自己運行DBConnection.php時,爲什麼這個命令(出現?)能夠正確工作是否有原因? –

+0

拉斐爾,謝謝!解決了問題#1!儘管如此,當我從「其他」文件夾運行「OMDb query.php」時,仍然會出現相同的404錯誤。對此有何想法? –

+0

我認爲你需要提供更多的信息。新的錯誤行是什麼意思?也許它可能是一個簡單的文件引用錯誤。另外,避免在.php文件名中使用空格。也許你試圖在瀏覽器上訪問它,但它沒有找到你的文件,因此404錯誤。 –

相關問題