Upgrade to Pro — share decks privately, control downloads, hide ads and more …

HTTP通信に使おうとすると実はめんどくさいfile_get_contents

 HTTP通信に使おうとすると実はめんどくさいfile_get_contents

PHPカンファレンス新潟2026の登壇資料です。
https://fortee.jp/phpconniigata-2026/proposal/603ce9bd-93ee-4988-89f6-6275e938dbee

マグロ隊長kinTV

November 14, 2026

Transcript

27 slides
  1. HTTP通信する方法 $context = stream_context_create([ 'http' => [ 'method' => 'POST',

    'header' => implode("\r\n", [ 'Content-Type: application/json', 'Authorization: Bearer secret-token', ]), 'content' => json_encode($data), 'timeout' => 3.0, 'ignore_errors' => true, ], ]); $body = file_get_contents($url, false, $context);
  2. HTTP通信する方法 $context = stream_context_create([ 'http' => [ 'method' => 'POST',

    'header' => implode("\r\n", [ 'Content-Type: application/json', 'Authorization: Bearer secret-token', ]), 'content' => json_encode($data), 'timeout' => 3.0, http通信のための設定をカスタ ム 'ignore_errors' => true, ], ]); $body = file_get_contents($url, false, $context); httpリクエストを送る
  3. HTTP通信する方法 $context = stream_context_create([ 'http' => [ 'method' => 'POST',

    'header' => implode("\r\n", [ 'Content-Type: application/json', 'Authorization: Bearer secret-token', ]), 'content' => json_encode($data), 'timeout' => 3.0, http通信のための設定をカスタ ム 'ignore_errors' => true, ], ]); $body = file_get_contents($url, false, $context); httpリクエストを送る
  4. HTTP通信する方法 $context = stream_context_create([ 'http' => [ 'method' => 'POST',

    'header' => implode("\r\n", [ 'Content-Type: application/json', 'Authorization: Bearer secret-token', ]), 'content' => json_encode($data), 'timeout' => 3.0, http通信のための設定をカスタ ム 'ignore_errors' => true, ], ]); $body = file_get_contents($url, false, $context); httpリクエストを送る
  5. HTTP通信する方法 $context = stream_context_create([ 'http' => [ ]), 便利そうに見えるけど 落とし穴があります

    'content' => json_encode($data), 'timeout' => 3.0, 'method' 'header' => 'POST', => implode("\r\n", [ 'Content-Type: application/json', 'Authorization: Bearer secret-token', http通信のための設定をカスタ ム 'ignore_errors' => true, ], ]); $body = file_get_contents($url, false, $context); httpリクエストを送る
  6. 状態の取り出しが複雑になりやすい ステータスコードを取り出す場合 $headers = http_get_last_response_headers(); // PHP 8.4+ (それ以前は $http_response_header)

    preg_match('{^HTTP/\S+\s+(\d{3})}', $headers[0], $m); $status = (int)$m[1]; "HTTP/1.1 200 OK"から200を 取り出す intに変換
  7. 状態の取り出しが複雑になりやすい Content-Typeを取り出す場合 foreach ($headers as $line) { if (stripos($line, 'Content-Type:')

    === 0) { $contentType = trim(substr($line, strlen('Content-Type:'))); } } 項目をトリミング (application/json) headersから”Content-Type: application/json”を探す
  8. 状態の取り出しが複雑になりやすい Content-Typeを取り出す場合 foreach ($headers as $line) { 読み辛い if (stripos($line,

    'Content-Type:') === 0) { $contentType = trim(substr($line, strlen('Content-Type:'))); } } 項目をトリミング (application/json) headersから”Content-Type: application/json”を探す
  9. 状態の取り出しが複雑になりやすい Content-Typeを取り出す場合 foreach ($headers as $line) { めんどくさい if (stripos($line,

    'Content-Type:') === 0) { $contentType = trim(substr($line, strlen('Content-Type:'))); } } 項目をトリミング (application/json) headersから”Content-Type: application/json”を探す
  10. 代替 httpリクエストの代替えとしてはcurlがおすすめ $ch = curl_init($url); curl_setopt_array($ch, [ CURLOPT_POST => true,

    CURLOPT_POSTFIELDS => json_encode($data), CURLOPT_HTTPHEADER => ['Content-Type: application/json', 'Authorization: Bearer secret-token'], CURLOPT_RETURNTRANSFER => true, CURLOPT_TIMEOUT => 3, ]); $body = curl_exec($ch); $status = curl_getinfo($ch, CURLINFO_RESPONSE_CODE); $contentType = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
  11. 代替 httpリクエストの代替えとしてはcurlがおすすめ $ch = curl_init($url); curl_setopt_array($ch, [ CURLOPT_POST => true,

    CURLOPT_POSTFIELDS => json_encode($data), CURLOPT_HTTPHEADER => ['Content-Type: application/json', 'Authorization: Bearer secret-token'], CURLOPT_RETURNTRANSFER => true, CURLOPT_TIMEOUT => 3, ]); $body = curl_exec($ch); $status = curl_getinfo($ch, CURLINFO_RESPONSE_CODE); $contentType = curl_getinfo($ch, CURLINFO_CONTENT_TYPE); file_get_contentsよりは簡単に ステータスが取れる
  12. 代替 httpリクエストの代替えとしてはcurlがおすすめ // 404 Not Found echo $body: // {"error":"page

    not found","code":"E1001"} // 接続拒否 echo curl_errno($ch); // 7 // DNS 解決失敗 echo curl_error($ch): // Could not resolve host: no-such-host.invalid エラーハンドリングも追いやすい

More Decks by マグロ隊長kinTV

Other Decks in Technology