Skip to main content

use file_get_contents with POST and GET

usually in PHP we are using cURL to execute GET/ POST/ DELETE/ methods but now I will mention here how to use it in file_get_contents

 

 

 

$request_url = {your url here};
$accessToken = {token here}
$method = {your method} // POST| GET etc..
data = {json data here}
$options = array(
    'http' => array(
        'header'  => [sprintf("Authorization: Bearer %s",$accessToken),'Content-Type:application/json'],
        'method'  => $method,
        'content' => $data
    )
);

$context  = stream_context_create($options);
$result = file_get_contents($request_url, false, $context);

 

Examples

 

$options = array(
    'http' => array(
        'header'  => ['Content-Type:application/json'],
        'method'  => "POST",
        'content' => '{json data}'
    )
);

$context  = stream_context_create($options);
$result = file_get_contents($request_url, false, $context);

 

Tags