2017-07-12 72 views
-1

在OSX上運行,在PHP 5.6.30上。我試圖從一個html文件執行一個腳本,然後它下載文件。注意:這不能正常工作,因爲html文件應該是.php,但這也是下載而不是打開它。下載文件而不是執行

我曾嘗試:

  • 在取消LoadModule php5_module libexec/apache2/libphp5.so
  • 加入AddHandler application/x-httpd-php5 .php/etc/apache2/httpd.conf
  • 重啓動Apache(sudo /usr/sbin/apachectl restart
  • 我的文件移動到www/script.php
  • 檢查script.php可執行

你可以在https://reteps.github.io/website.html看到它,而我只在谷歌瀏覽器上測試過。

HTML(應該是.PHP)

<!doctype html> 
<html> 
    <head> 
    <title> 

    </title> 
    <link rel="stylesheet" type="text/css" href="navbar.css"> 
    </head> 
    <body> 

    <div id="menu"> 
     <a href="index.html">Home</a> 
     <a href="https://github.com/reteps">Github</a> 
     <a href="school.html">School Projects</a> 
     <a href="side.html" >Side Projects</a> 
     <a href="blog.html">Blog</a> 
     </div> 
    <form action="www/script.php" method="get"> 
    <input type="text" name="quizid" placeholder="kahoot id"> 
    <input type="submit" value="Go"> 
    </form> 
    <p id="output">Nothing yet...</p> 
    </body> 
</html> 

PHP

#!/usr/bin/php 
<?php 
$username = '[email protected]'; 
$password = 'PASSWORD'; 
$loginUrl = 'https://create.kahoot.it/rest/authenticate'; 
$kahootId = htmlentities($_GET['quizid']); 
$pageUrl = 'https://create.kahoot.it/rest/kahoots/' . $kahootId; 

$loginheader = array(); 
$loginheader[] = 'content-type: application/json'; 
$loginpost = new stdClass(); 
$loginpost->username = $username; 
$loginpost->password = $password; 
$loginpost->grant_type = "password"; 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $loginUrl); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($loginpost)); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); 
curl_setopt($ch, CURLOPT_HTTPHEADER,$loginheader); 
$store = curl_exec($ch); 
curl_close($ch); 
$token = json_decode($store,true)["access_token"]; 

//get questions 
$quizheader = array(); 
$quizheader[] = 'authorization: ' . $token; 
$options = array(
    'http' => array(
     'method' => 'GET', 
     'header' => "Authorization: ".$token."\r\n" 
    ) 
); 

$context = stream_context_create($options); 
$raw_result = file_get_contents($pageUrl, false, $context); 
$result = json_decode($raw_result,true)["questions"]; 
$myoutput = $_POST['output']; 
header("Location: website.html?output=$myoutput"); 
print_r($result) 
?> 

回答

相關問題