0

I am trying to create a service on hook.io to load token from another 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 from the server
                echo($obj);
                return NULL;
            }

            return $obj;
        }

But it turns out hook.io doesn't support cUrl in PHP . I know it can done directly with php but i don't know how.

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

$username = '';
$password = '';
$authServiceUrl = '';



$computedHash = base64_encode(hash_hmac ( 'md5' , $authServiceUrl , $password, true ));
$authorization = 'Authorization: Bearer '.$username.':'.$computedHash;

// Create map with request parameters


// Build Http query using params


// Create Http context details
$contextData = array ( 
                'method' => 'POST',
                'header' => "Content-Type: application/json". $authorization ,
                 );

// Create context resource for our request
$context = stream_context_create (array ( 'http' => $contextData ));

// Read page rendered as result of your POST request
$result =  file_get_contents (
                  $authServiceUrl,  // page url
                  false,
                  $context);

I have added the error in comments below

  • http://stackoverflow.com/questions/11468720/curl-alternatives-to-get-post-answer-on-a-webpage – Janno Jul 28 '16 at 11:01

1 Answers1

0

Actually, hook.io has curl support as far as I know. But if you want to try other alternatives, you can use file_get_contents. It has support for custom context and 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);
Tuğca Eker
  • 1,493
  • 13
  • 20
  • PHP Fatal error: Call to undefined function curl_init() in /bin/run-hook-php(43) : eval()'d code(153) : eval()'d code on line 39 – Siddharth Hemrajani Jul 28 '16 at 11:15
  • this was the error i got when i hit test code at hook.io, – Siddharth Hemrajani Jul 28 '16 at 11:16
  • I see. Your microservice / container etc. may not have curl library. You can use `php_info()` to inspect installed libraries. Also, did you try `file_get_content`? – Tuğca Eker Jul 28 '16 at 11:24
  • PHP Warning: file_get_contents(https://sandbox-authservice.priaid.ch/login): failed to open stream: HTTP request failed! HTTP/1.1 411 Length Required in /bin/run-hook-php(43) : eval()'d code(107) : eval()'d code on line 29 – Siddharth Hemrajani Jul 28 '16 at 14:08