Alternative way to write cUrl in PHP -


i trying create service on hook.io load token api.

public function loadtoken()         {             $computedhash = base64_encode(hash_hmac ( 'md5' , $this->authserviceurl , $this->password, true ));             $authorization = 'authorization: bearer '.$this->username.':'.$computedhash;              $curl = curl_init();             curl_setopt($curl, curlopt_post, true);             curl_setopt($curl, curlopt_postfields, '');             curl_setopt($curl, curlopt_httpheader, array('content-type: application/json' , $authorization ));             curl_setopt($curl, curlopt_url, $this->authserviceurl);             curl_setopt($curl, curlopt_returntransfer, 1);             curl_setopt($curl, curlopt_ssl_verifypeer, false);              $result = curl_exec($curl);             $obj = json_decode($result);             $info = curl_getinfo($curl);             curl_close($curl);              if($info['http_code'] != '200')             {                 // print error server                 echo($obj);                 return null;             }              return $obj;         } 

but turns out hook.io doesn't support curl in php . know can done directly php don't know how.

edit : used file_get_contents it's give other error now.

$username = ''; $password = ''; $authserviceurl = '';    $computedhash = base64_encode(hash_hmac ( 'md5' , $authserviceurl , $password, true )); $authorization = 'authorization: bearer '.$username.':'.$computedhash;  // create map request parameters   // build http query using params   // create http context details $contextdata = array (                  'method' => 'post',                 'header' => "content-type: application/json". $authorization ,                  );  // create context resource our request $context = stream_context_create (array ( 'http' => $contextdata ));  // read page rendered result of post request $result =  file_get_contents (                   $authserviceurl,  // page url                   false,                   $context); 

i have added error in comments below

actually, hook.io has curl support far know. if want try other alternatives, can use file_get_contents. has support custom context , parameters.

$opts = [     'http' => [             'method' => 'post',             'headers' => ['authorization' => 'bearer ' . $this->username . ':' . $computedhash]     ] ]; $context = stream_context_create($opts); $file = file_get_contents('http://www.hook.io/example/', false, $context); 

Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -