405 error php get request

无人久伴 提交于 2020-01-06 05:18:04

问题


I have this script and I want to make a request to a page, but it gives me a 405 help. Here is my code:

$result = "SOMETHING"
$kahootId = '0c17fb60-76c6-424c-9326-d1154cbc70d3';
$pageUrl = 'https://create.kahoot.it/rest/kahoots/' . $kahootId;
$quizheader = array(); 
$quizheader[] = 'content-type: application/json';
$quizheader[] = 'authorization: ' . $result;

curl_setopt($ch, CURLOPT_URL, $pageUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); 
curl_setopt($ch, CURLOPT_HEADER,$quizheader);

$store2 = curl_exec($ch);
print_r($store2);
curl_close($ch);

This code returns:

HTTP/1.1 405 Method Not Allowed
Server: openresty/1.11.2.2
Date: Tue, 11 Jul 2017 20:53:03 GMT
Content-Type: application/json
Content-Length: 150
Connection: keep-alive

{"error":"javax.ws.rs.WebApplicationException","exception":"javax.ws.rs.WebApplicationException","timestamp":1499806383136,"duration":0,"errorCode":0} 

This code was based off of this python script:


回答1:


If all you need is a GET request with custom headers and no body, you can use the following method that doesn't even need Curl:

$token = "SOMETHING"
$kahootId = '0c17fb60-76c6-424c-9326-d1154cbc70d3';
$url = 'https://create.kahoot.it/rest/kahoots/'.$kahootId;

$options = array(
    'http' => array(
        'method'  => 'GET',
        'header'  => "Authorization: ".$token."\r\n"
    )
);

$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);


来源:https://stackoverflow.com/questions/45044027/405-error-php-get-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!