note 124327 deleted from function.file-get-contents by salathe
| From: | salathe@php.net | Date: | Mon, 21 Sep 2020 14:37:15 +0000 |
| Subject: | note 124327 deleted from function.file-get-contents by salathe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-215222@lists.php.net to get a copy of this message | ||
Note Submitter: miszcz-anonym at email dot xxx
----
Small function for http post with json, auth and bearer token, iframes, allow orgin
<?php
$url = 'http://test.xx/api.php';
// Return array with content and headers
$res = HttpPost($url, array('name' => 'Jombo', 'id' => 247),
true);
print_r($res);
function HttpPost ($url, $data, $json = false, $selfsigned = false, $token = '', $user =
'', $pass = ''){
$data_url = http_build_query ($data);
$data_len = strlen ($data_url);
if($json == true){
// text/html,
// content-type application/x-www-form-urlencoded
$content = "Content-type: application/json; charset=utf-8";
$data_url = json_encode($data);
$data_len = strlen ($data_url);
}
if(!empty($user) && !empty($pass)){
$auth = "Authorization: Basic
".base64_encode("$user:$pass")."\r\n";
}
if(!empty($token)){
$token = "Authorization: Bearer ".$token;
}
if($selfsigned == true){
$ssl = array(
"verify_peer" => false,
"allow_self_signed" => true,
);
}else{
$ssl = array(
"verify_peer" => true,
"allow_self_signed" => false,
);
}
return array (
'content' =>
file_get_contents (
$url,
false,
stream_context_create (
array (
'ssl' => $ssl,
'http'=>
array (
'timeout' => 60,
'method'=> 'POST'
, 'header'=>
$content .
"Access-Control-Allow-Origin: *".
"X-Frame-Options: sameorigin".
"Connection: close\r\n"
."Content-Length: $data_len\r\n"
.$auth
.$token
, 'content'=>$data_url
)
)
)
)
,'headers'=>$http_response_header
);
}
?>
<?php
// api.php file
function api(){
if(!empty($_POST)){
print_r($_POST);
}else{
echo file_get_contents('php://input');
}
}
?>