Last active
March 15, 2019 11:26
-
-
Save abdullahbutt/232fd882e55c378083f6d8a621fb2568 to your computer and use it in GitHub Desktop.
api call file_get_contents with headers
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 | |
$arrContextOptions = [ | |
"ssl"=> [ | |
"verify_peer"=>false, | |
"verify_peer_name"=>false, | |
], | |
"http" => [ | |
"method" => "GET", | |
"header" => [ | |
"Authorization: yd5e63asd409acadasdase26adasdas" , | |
"X-com-zoho-subscriptions-organizationid: 123456789" , | |
"Content-Type: application/json;charset=UTF-8" | |
] | |
] | |
]; | |
$url = "https://subscriptions.zoho.com/api/v1/customers"; | |
$data = file_get_contents($url, false, stream_context_create($arrContextOptions)); // this WILL do an http request for you | |
//echo '<pre>';var_dump($data->customers);echo '</pre>';die(); | |
$updatedData = json_decode($data); | |
dd($updatedData->customers); | |
//-------------------- Alternate Approach ----------------------------------- | |
/* | |
https://stackoverflow.com/questions/28134446/how-to-add-multiple-headers-to-file-get-content-in-php | |
When you pass multiple headers there should two \r\n before sending POST data. | |
Let me explain. | |
Try this: | |
*/ | |
$opts = array( | |
'http' => array( | |
'method' => $_SERVER['REQUEST_METHOD'], | |
'header' => array( | |
"Accept-language: en\r\n", | |
"Cookie: ".session_name()."=".session_id()."\r\n", | |
"Content-type: application/x-www-form-urlencoded\r\n\r\n" | |
), | |
'content' => $_POST | |
) | |
); | |
$postdata = http_build_query($_POST); | |
$context = stream_context_create($opts); | |
session_write_close(); // this is the key | |
echo $obsah = file_get_contents("http://localhost/journal/", false, $context); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment