Making Requests with file_get_contents
GET Request
$options=[
'http'=>[
'method' => 'GET',
'timeout'=>10, //Timeout in seconds
'ignore_errors' => true, //Retrieve content even if status code is 4xx or 5xx
]
];
$context=stream_context_create($options);
//Specify the URL you want to request
$url='https://mam-mam.net/';
$res=file_get_contents($url,false,$context);
//Extract the HTTP response code from the first response header line
preg_match('/^HTTP\/1\.[0|1|x] ([0-9]+) (.*)$/',$http_response_header[0],$matches);
$http_response_code=$matches[1]; //HTTP response code
$http_response_text=$matches[2]; //HTTP response text
if($res){echo $res;}else{echo "error\n";}
POST Request
//Data to send via POST
$data=['a'=>'ABCDEFG','b'=>'1'];
//Convert character encoding if required by the destination
mb_convert_variables('UTF-8', 'UTF-8', $data);
//Build query string
$data = http_build_query($data, "", "&");
$options=[
'http'=>[
'method' => 'POST',
'timeout'=>10, //Timeout in seconds
'header' => 'Content-Type: application/x-www-form-urlencoded'."\r\n".
'Content-Length: '.strlen($data)."\r\n".
'Cache-Control: no-cache',
'content' => $data,
'ignore_errors' => true, //Retrieve content even if status code is 4xx or 5xx
]
];
$context=stream_context_create($options);
//Specify the URL you want to request
$url='https://mam-mam.net/';
$res=file_get_contents($url,false,$context);
//Extract the HTTP response code from the first response header line
preg_match('/^HTTP\/1\.[0|1|x] ([0-9]+) (.*)$/',$http_response_header[0],$matches);
$http_response_code=$matches[1]; //HTTP response code
$http_response_text=$matches[2]; //HTTP response text
if($res){echo $res;}else{echo "error\n";}
GET Request with Basic Authentication
$options=[
'http'=>[
'method' => 'GET',
'timeout'=>10, //Timeout in seconds
'header'=>'Authorization: Basic '.base64_encode('UserName'.':'.'Password')."\r\n"
'ignore_errors' => true, //Retrieve content even if status code is 4xx or 5xx
]
];
$context=stream_context_create($options);
//Specify the URL you want to request
$url='https://mam-mam.net/';
$res=file_get_contents($url,false,$context);
//Extract the HTTP response code from the first response header line
preg_match('/^HTTP\/1\.[0|1|x] ([0-9]+) (.*)$/',$http_response_header[0],$matches);
$http_response_code=$matches[1]; //HTTP response code
$http_response_text=$matches[2]; //HTTP response text
if($res){echo $res;}else{echo "error\n";}
GET Request to an HTTPS Site with an Invalid Certificate
$options=[
'http'=>[
'method' => 'GET',
'timeout'=>10, //Timeout in seconds
'ignore_errors' => true, //Retrieve content even if status code is 4xx or 5xx
],
'ssl'=>[
'verify_peer'=>false,
'verify_peer_name'=>false,
]
];
$context=stream_context_create($options);
//Specify the URL you want to request
$url='https://mam-mam.net/';
$res=file_get_contents($url,false,$context);
//Extract the HTTP response code from the first response header line
preg_match('/^HTTP\/1\.[0|1|x] ([0-9]+) (.*)$/',$http_response_header[0],$matches);
$http_response_code=$matches[1]; //HTTP response code
$http_response_text=$matches[2]; //HTTP response text
if($res){echo $res;}else{echo "error\n";}
GET Request to an HTTPS Site with an Invalid Certificate (Using a Server Certificate File)
$options=[
'http'=>[
'method' => 'GET',
'timeout'=>10, //Timeout in seconds
'ignore_errors' => true, //Retrieve content even if status code is 4xx or 5xx
],
'ssl'=>[
'verify_peer'=>true,
'cafile'=>'/home/hoge.csr', //Server certificate file
]
];
$context=stream_context_create($options);
//Specify the URL you want to request
$url='https://mam-mam.net/';
$res=file_get_contents($url,false,$context);
//Extract the HTTP response code from the first response header line
preg_match('/^HTTP\/1\.[0|1|x] ([0-9]+) (.*)$/',$http_response_header[0],$matches);
$http_response_code=$matches[1]; //HTTP response code
$http_response_text=$matches[2]; //HTTP response text
if($res){echo $res;}else{echo "error\n";}
When a Client Certificate Is Required for a GET Request
Use a client certificate that has been converted to PEM format.
$options=[
'http'=>[
'method' => 'GET',
'timeout'=>10, //Timeout in seconds
'ignore_errors' => true, //Retrieve content even if status code is 4xx or 5xx
],
'ssl'=>[
'verify_peer'=>false,
'verify_peer_name'=>false,
'local_cert', '/home/hoge.pem', //Client certificate (PEM format)
'passphrase'=>'PassPhrase',
]
];
$context=stream_context_create($options);
//Specify the URL you want to request
$url='https://mam-mam.net/';
$res=file_get_contents($url,false,$context);
//Extract the HTTP response code from the first response header line
preg_match('/^HTTP\/1\.[0|1|x] ([0-9]+) (.*)$/',$http_response_header[0],$matches);
$http_response_code=$matches[1]; //HTTP response code
$http_response_text=$matches[2]; //HTTP response text
if($res){echo $res;}else{echo "error\n";}