2016-07-28 4 views
0

Ich versuche, einen Service auf hook.io zu erstellen, um Token von einer anderen API zu laden.Alternative Möglichkeit zum Schreiben von cUrl in PHP

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; 
     } 

Aber es stellt sich heraus, hook.io unterstützt nicht cUrl in PHP. Ich weiß, dass es direkt mit PHP gemacht werden kann, aber ich weiß nicht wie.

edit: ich benutzte file_get_contents jetzt gibt es einen anderen Fehler jetzt.

$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); 

Ich habe den Fehler in den Kommentaren unten

+0

http://stackoverflow.com/questions/11468720/curl-alternatives-to-get-post-answer-on-a-webpage – Janno

Antwort

0

Eigentlich hinzugefügt hat hook.io curl Unterstützung soweit ich weiß. Aber wenn Sie andere Alternativen ausprobieren möchten, können Sie file_get_contents verwenden. Es unterstützt benutzerdefinierte Kontext und Parameter.

$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); 
+0

PHP Fatal error: Call to undefined Funktion curl_init() in/bin/run-hook-php (43): eval() 'd-code (153): eval()' d-code in der zeile 39 –

+0

das war der fehler, den ich bekam, als ich den testcode bei hook.io, –

+0

sah. Ihr Microservice/Container etc. darf keine Curl-Bibliothek haben. Sie können 'php_info()' verwenden, um installierte Bibliotheken zu überprüfen. Haben Sie auch 'file_get_content' versucht? –

Verwandte Themen