Skip to content

Instantly share code, notes, and snippets.

@kobake
Last active December 18, 2015 19:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kobake/5836879 to your computer and use it in GitHub Desktop.
Save kobake/5836879 to your computer and use it in GitHub Desktop.
Qiita API を使うときには User-Agent を設定しないと 500 Internal Server Error が発生する ref: http://qiita.com/kobake@github/items/d256fd1665284fd3c65a
$ php QiitaApiSimple.php
Warning: file_get_contents(https://qiita.com/api/v1/items): failed to open stream: HTTP request failed! HTTP/1.1 500 Internal Server Error
in QiitaApiSimple.php on line 3
Call Stack:
0.0000 338728 1. {main}() QiitaApiSimple.php:0
0.0000 338880 2. file_get_contents() QiitaApiSimple.php:3
PHP Warning: file_get_contents(https://qiita.com/api/v1/items): failed to open stream: HTTP request failed! HTTP/1.1 500 Internal Server Error
in QiitaApiSimple.php on line 3
PHP Stack trace:
PHP 1. {main}() QiitaApiSimple.php:0
PHP 2. file_get_contents() QiitaApiSimple.php:3
<?php
$url = 'https://qiita.com/api/v1/items';
$opts = array(
'http'=>array(
'method'=> "GET",
'header'=> "User-Agent: OreOreAgent\r\n"
)
);
$context = stream_context_create($opts);
$response = file_get_contents($url, false, $context);
user_agent='OreOreAgent' (追記)
<?php
$url = 'https://qiita.com/api/v1/items';
$opts = array(
'http'=>array(
'method'=> "GET",
'header'=> "User-Agent: OreOreAgent\r\n"
)
);
$context = stream_context_create($opts);
$response = file_get_contents($url, false, $context);
<?php
ini_set('user_agent', 'OreOreAgent');
$url = 'https://qiita.com/api/v1/items';
$response = file_get_contents($url);
<?php
$url = 'https://qiita.com/api/v1/items';
$response = file_get_contents($url);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment