我正在阅读question (Request forbidden while accessing github api on node.js program)的过去,但仍然无法解决问题。
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => "https://api.github.com/markdown/raw",
CURLOPT_HEADER => [
"Accept: application/vnd.github.v3+json",
"Content-Type: text/plain",
"User-Agent: mfmfneko"
],
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => "# Hello World!"
]);
$response = curl_exec($ch);
curl_close($ch);
print($response);
HTTP/1.0 403 Forbidden
Cache-Control: no-cache
Connection: close
Content-Type: text/html
Request forbidden by administrative rules. Please make sure your request has a User-Agent header (http://developer.github.com/v3/#user-agent-required). Check https://developer.github.com for other possible causes.
答案 0 :(得分:2)
你在安装标题时犯了错误。
CURLOPT_HEADER TRUE,在输出中包含标题。
CURLOPT_HTTPHEADER要设置的HTTP标头字段数组 格式数组('内容类型:text / plain','内容长度:100')
应使用CURLOPT_HTTPHEADER
报告它们试试这段代码:
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => "https://api.github.com/markdown/raw",
CURLOPT_HTTPHEADER => [
"Accept: application/vnd.github.v3+json",
"Content-Type: text/plain",
"User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.111 YaBrowser/16.3.0.7146 Yowser/2.5 Safari/537.36"
],
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => "# Hello World!"
]);
$response = curl_exec($ch);
curl_close($ch);
print($response);
响应:
<h1>
<a id="user-content-hello-world" class="anchor" href="#hello-world" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Hello World!</h1>