405错误php获取请求

时间:2017-07-11 20:35:09

标签: php get http-status-code-405

我有这个脚本,我想向页面发出请求,但它给了我405帮助。这是我的代码:

$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);

此代码返回:

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} 

此代码基于此python脚本: enter image description here

1 个答案:

答案 0 :(得分:0)

如果你需要的只是一个带有自定义标题且没有正文的GET请求,你可以使用以下方法,甚至不需要使用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);