note 124327 added to function.file-get-contents

From: Date: Wed, 23 Oct 2019 11:18:45 +0000
Subject: note 124327 added to function.file-get-contents
Groups: php.notes 
Request: Send a blank email to php-notes+get-212755@lists.php.net to get a copy of this message
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'); } } ?> ---- Server IP: 208.43.231.9 Probable Submitter: 185.85.3.168 (proxied: 89.230.41.149) ---- Manual Page -- http://php.net/manual/en/function.file-get-contents.php Edit -- https://master.php.net/note/edit/124327 Del: integrated -- https://master.php.net/note/delete/124327/integrated Del: useless -- https://master.php.net/note/delete/124327/useless Del: bad code -- https://master.php.net/note/delete/124327/bad+code Del: spam -- https://master.php.net/note/delete/124327/spam Del: non-english -- https://master.php.net/note/delete/124327/non-english Del: in docs -- https://master.php.net/note/delete/124327/in+docs Del: other reasons-- https://master.php.net/note/delete/124327 Reject -- https://master.php.net/note/reject/124327 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#212755) next »