Last active
December 23, 2019 00:47
-
-
Save hrysd/f30880706698762cb3661478f8e14fc6 to your computer and use it in GitHub Desktop.
PHP 標準の cURL の挙動
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'bundler/inline' | |
gemfile do | |
source 'https://rubygems.org' | |
gem 'sinatra' | |
end | |
require 'sinatra' | |
use Rack::Auth::Basic do |username, password| | |
username == 'hoge' && password == 'huga' | |
end | |
get '/gohyaku' do | |
halt 500 | |
end | |
get '/yonhyaku' do | |
halt 404 | |
end | |
get '/json' do | |
{message: 'Up yours!'}.to_json | |
end | |
get '/json-with-type' do | |
content_type :json | |
{message: 'Vindication!!!!!!'}.to_json | |
end | |
post '/post-me' do | |
params.tap { |it| p it }.inspect | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
# 200 | |
$ch1 = curl_init('http://localhost:4567/json'); | |
# 200 with type | |
$ch2 = curl_init('http://localhost:4567/json-with-type');; | |
# 40X | |
$ch3 = curl_init('http://localhost:4567/yonhyaku'); | |
# 50X | |
$ch4 = curl_init('http://localhost:4567/gohyaku'); | |
# URL がおかしい | |
$ch5 = curl_init('htt://hoge.Y'); | |
# つながらい | |
$ch6 = curl_init('http://localhost:4568'); | |
$ch7 = curl_init('http://localhost:4567/post-me'); | |
foreach ([$ch1, $ch2, $ch3, $ch4, $ch5, $ch6] as $c) { | |
curl_setopt_array($c, [ | |
CURLOPT_RETURNTRANSFER => true, | |
CURLOPT_USERPWD => "hoge:huga" | |
]); | |
$result = curl_exec($c); | |
var_dump($result, curl_getinfo($c, CURLINFO_HTTP_CODE), curl_error($c)); | |
curl_close($c); | |
} | |
curl_setopt_array($ch7, [ | |
CURLOPT_RETURNTRANSFER => true, | |
CURLOPT_USERPWD => "hoge:huga", | |
CURLOPT_POSTFIELDS => [ | |
'hey' => 'whats up' | |
] | |
]); | |
$result = curl_exec($ch7); | |
var_dump($result, curl_getinfo($ch7, CURLINFO_HTTP_CODE), curl_error($ch7)); | |
curl_close($ch7); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment