Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am currently getting this error: :60018/server.php Failed to load resource: the server responded with a status of 405 (Method Not Allowed)

I am not very familiar with php code as this is code a friend wrote a while ago for a project we worked on, and I wanted to work on that project on my own a bit and make it better. But I am unsure what is making this happen, I have tried looking up stuff about it but I honestly haven't used any php before. The code is below, any help and ideas would be appreciated. And in case it matters, I am running this project on Visual Studios 2015

$ids = buildFinalList();
echo json_encode($ids);

function buildFinalList() {
    $conn = dbConnect();
    $type = 'themoviedb';
    $input = json_decode(file_get_contents('php://input'), true);
    $genre = $input['genres'];
    $sources = $input['sources'];
    $page = 1;

    $result = array();

    while (count($result) <= 2) {

        $currentPage = generateList($genre, $page++);
        $idChecked = idCheck($conn, $currentPage, $type);
        $sourceChecked = sourceCheck($conn, $idChecked, $sources);
        $result = calcRating($sourceChecked, $result);
    }

    $conn->close();

    $filmIDs = array();

    foreach ($result as $key => $value) {
        array_push($filmIDs, $key);
    }

    return $filmIDs;
}

function generateList($genreID, $pageNum) {
    $usURL = "http://api.themoviedb.org/3/discover/movie?";
    $apiKey = "api_key=9372ef92347b5703a397967af35f6c1c";
    $genre = "&with_genres=" . $genreID;
    $page = "&page=" . $pageNum;
    $votes = "&vote_count.gte=50";
    $endURL = "&sort_by=vote_average.desc";
    $data1 = json_decode(file_get_contents($usURL.$apiKey.$genre.$page.$votes.$endURL), true);
    $data2 = $data1['results'];
    $ids = array();
    foreach ($data2 as $result) {
        array_push($ids, $result['id']);
    }
    return $ids;
}

function dbConnect() {
    $conn = new mysqli("localhost", "root", "", "movies");
    if ($conn->connect_error) {
        exit("Server connection error");
    }
    return $conn;
}

function idCheck($conn, $ids, $type) {

    $results = array();

    foreach ($ids as $id) {
        //database check
        $query = "SELECT * FROM `filmids` WHERE ".$type."=\"".$id."\"";
        $result = $conn->query($query);

        if ($result->num_rows == 0) {
            $guideCall = json_decode(file_get_contents
            ('http://api-public.guidebox.com/v1.43/US/rKs3n3vENgt7CQC6D2W8y8tQYtrVoaAx/search/movie/id/'
                .$type.'/'.$id), true);

            if (empty($guideCall) || empty($guideCall['imdb'])){
                continue;
            } else {
                $in = "INSERT INTO `filmids` (`imdb`, `themoviedb`, `guide`) VALUES (\""
                    .$guideCall["imdb"]."\", \"".$guideCall["themoviedb"]."\", \""
                    .$guideCall["id"]."\")";
                $conn->query($in);
                $out = "SELECT * FROM `filmids` WHERE ".$type."=\"".$id."\"";
                $result = $conn->query($out);
                array_push($results, $result->fetch_assoc());
            }
        } else {
            array_push($results, $result->fetch_assoc());
        }
    }

    return $results;
}

function sourceCheck($conn, $ids, $sources) {

    $validFilms = array();

    foreach ($ids as $film) {
        $query = "SELECT * FROM `sources` WHERE id=".$film['id'];
        $result = $conn->query($query);

        if ($result->num_rows == 0) {
            $addRow = "INSERT INTO `sources`(`id`) VALUE (".$film['id'].")";
            $conn->query($addRow);

            $guideCall = json_decode(file_get_contents
            ('http://api-public.guidebox.com/v1.43/US/rKs3n3vENgt7CQC6D2W8y8tQYtrVoaAx/movie/'
                . $film['guide']), true);

            $webSources = $guideCall['subscription_web_sources'];
            $trueSources = array();

            foreach ($webSources as $webSource) {
                array_push($trueSources, $webSource['source']);
            }

            foreach ($trueSources as $trueSource) {
                $query = "UPDATE `sources` SET `".$trueSource."` = \"".$trueSource."\" WHERE `id`=".$film['id'];
                $check = $conn->query($query);
                if ($check) {
                    continue;
                } else {
                    $conn->query("ALTER TABLE `sources` ADD `".$trueSource."` VARCHAR(255)");
                    $conn->query($query);
                }
            }

            if (count(array_diff($sources, $trueSources)) < count($sources)) {
                array_push($validFilms, $film);
            }

        } else {
            if (count(array_diff($sources, $result->fetch_assoc())) < count($sources)) {
                array_push($validFilms, $film);
            }
        }
    }
    return $validFilms;
}

function calcRating($films, $sorted) {

    foreach ($films as $film) {

        $OMDb = json_decode(file_get_contents("http://www.theimdbapi.org/api/movie?movie_id=".$film['imdb']), true);

        $rating = ($OMDb['rating']);

        $film['rating'] = $rating;

        $sorted[$film['imdb']] = $rating;

    }

    arsort($sorted);

    return $sorted;

}


What I have tried:

I have php and IIS installed and configured correctly, made sure the handlers had the POST method allowed like they should, added the .php MIME type in both the IIS manager and the applicationhost.config file
Posted
Updated 19-Aug-17 17:17pm
Comments
Richard MacCutchan 20-Aug-17 12:41pm    
Which line causes the error?
Member 13368942 21-Aug-17 9:45am    
At first it doesn't give me the line for an erorr, it just says the error is in the serber.php file. However, after adding a phone MIME type, now when it gives me the 405 erorr, it says the error is: "angular.js:11756 POST localhost:60018/server.php 405 (Method Not Allowed)"
Richard MacCutchan 21-Aug-17 11:15am    
So that is telling you that your server cannot handle the request you are trying to send it. You need to look into the request details and see why the server rejects it.
Member 13368942 21-Aug-17 12:35pm    
I have no idea how to look into the request details to see why the server would reject it
Richard MacCutchan 21-Aug-17 13:41pm    
Well it is something you need to learn if you aspire to be a software developer.

1 solution

Have a look at server, there should be some report about the error including detail like position of error.
----
Not a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]
 
Share this answer
 
v2
Comments
Member 13368942 21-Aug-17 9:40am    
I do not know what you mean by having a look at the server. I am running the application on Visual Studios by pushing the IIS Express button that opens the website up in chrome, and then when I get to where the php should be loaded it gives me that 405 error in the console. I was told somewhere else it seems that my server doesn't recognize the php, but I'm not sure why as I've followed the php installation steps a few times and it still hasn't worked.
Patrice T 21-Aug-17 11:22am    
The IIS part simulating your server.
There should be some error log where IIS tell you what is wrong.
Member 13368942 21-Aug-17 12:35pm    
I have no idea how I would find that log

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900