2016-08-22 5 views
0

Ich habe einen Code, der eine Anfrage zum Abrufen von Daten von Jive API stellt. Ich lasse diesen Code auf MAMP laufen, der mir erlaubt, entweder PHP 5.6.10 oder PHP 7.0.0 zu laufen. Mit PHP5 bekomme ich eine erfolgreiche Antwort. Mit PHP 7 bekomme ich 401 Unauthorized.fopen Nein Authorization Header mit PHP 7

Die entsprechende Funktion ist hier:

protected function sendRequest($method, $url, $auth = null) { 
    global $CFG; 
    $options = func_num_args() === 4 ? func_get_arg(3) : array(); 

    $http = array(
     'max_redirects' => 0, 
     'request_fulluri' => 1, 
     'ignore_errors' => true, 
     'method' => $method, 
     'header' => array() 
    ); 

    if (!is_null($auth)) { 
     array_push($http['header'], 'Authorization: ' . $auth); 
    } 

    if (($method === 'PUT' || $method === 'POST') && isset($options['content'])) { 
     $http['content'] = $options['content']; 
     array_push($http['header'], 'Content-length: ' . strlen($options['content'])); 
     array_push($http['header'], 'Content-Type: application/json'); 
    } 

    var_dump($http);echo('<hr/>'); 

    $context = stream_context_create(array('http' => $http)); 

    var_dump(stream_context_get_options($context));echo('<hr/>'); 
    $fp = fopen($url, 'rb', false, $context); 
    if (! $fp) { 
     throw new \Exception('Request failed: $php_errormsg'); 
    } 
    $metadata = stream_get_meta_data($fp); 
    $content = stream_get_contents($fp); 
    $responseCode = (int)explode(' ', $metadata['wrapper_data'][0])[1]; 

    fclose($fp); 

    return array (
     'metadata' => $metadata, 
     'content' => $content, 
     'status' => $responseCode 
    ); 
} 

Die Var_dump Anrufe die gleichen Ergebnisse sowohl mit PHP-Versionen produzieren. Die Antwort, die ich bekommen ist:

{ 
    "metadata": { 
     "wrapper_data": [ 
      "HTTP/1.0 401 Unauthorized", 
      "Server: Apache", 
      "X-Jive-Request-Id: 6c433c20-688a-11e6-b332-005056a4250c", 
      "X-Jive-Flow-Id: 6c433c21-688a-11e6-b332-005056a4250c", 
      "X-Frame-Options: SAMEORIGIN", 
      "Expires: Mon, 22 Aug 2016 17:04:01 GMT", 
      "Cache-Control: no-store, no-cache, must-revalidate, private, max-age=0", 
      "X-JSL: D=1754 t=1471885441249342", 
      "Content-Type: text/plain", 
      "Date: Mon, 22 Aug 2016 17:04:01 GMT", 
      "Connection: close", 
      "Set-Cookie: jive.login.ts=1471885441250; Path=/; Secure; HttpOnly;HttpOnly", 
      "Set-Cookie: X-JCAPI-Token=pTVEn2P4; Path=/; Secure; HttpOnly", 
      "Set-Cookie: BIGipServerpool_sandbox.jiveon.com=25472522.20480.0000; path=/" 
     ], 
     "wrapper_type": "http", 
     "stream_type": "tcp_socket/ssl", 
     "mode": "rb", 
     "unread_bytes": 0, 
     "seekable": false, 
     "uri": "https://sandbox.jiveon.com/api/core/v3/activities?after=2016-08-22T17:01:14%2b0000&count=500", 
     "crypto": { 
      "protocol": "TLSv1", 
      "cipher_name": "ECDHE-RSA-AES256-SHA", 
      "cipher_bits": 256, 
      "cipher_version": "TLSv1/SSLv3" 
     }, 
     "timed_out": false, 
     "blocked": true, 
     "eof": false 
    }, 
    "content": "", 
    "status": 401, 
    "success": false 
} 

von https://requestb.in mit mir sehen kann, dass die PHP7 Version beinhaltet nicht die Header Authorization

Was zwischen PHP 5.6.10 und PHP 7 verursachen verändert? Wie repariere ich es?

BEARBEITEN: Entfernen Sie einige rote Hering Text und fügen Sie Anfrage Bin Ergebnis hinzu.

+0

Für den Anfang sehe ich "WWW-Authenticate: Basic Realm = \" Jive SBS \ "" 'in der PHP5-Anfrage, aber nicht die PHP7. Ich habe noch nie mit dieser API gearbeitet, vielleicht sollten Sie sich mit ihnen in Verbindung setzen. Es ist möglich, dass PHP7 verschiedene Header sendet, deren API sie für einen Hackerversuch hält. – MonkeyZeus

+0

Danke. Das gibt mir eine Idee. Ich versuche, die Anfrage an einen Endpunkt zu richten, den ich kontrolliere, und sehe, was anders ist. –

+0

NP, eine andere Sache zu prüfen wäre, CURL anstelle von fopen() zu verwenden, weil es aussieht, als ob Sie einfach mit einer JSON-API interagieren und fopen() ist nur ein Wrapper für CURL beim Umgang mit URLs. Ich bezweifle, dass es überhaupt Vorteile gibt, fopen() zu benutzen, aber ich bin nicht der Experte, also viel Glück! – MonkeyZeus

Antwort

0

Die folgenden Werke:

protected function sendRequest($method, $url, $auth = null) { 
    global $CFG; 
    $options = func_num_args() === 4 ? func_get_arg(3) : array(); 

    $http = array(
     'max_redirects' => 0, 
     'request_fulluri' => 1, 
     'ignore_errors' => true, 
     'method' => $method 
    ); 

    $headers = array(); 

    if (!is_null($auth)) { 
     array_push($headers, 'Authorization: ' . $auth); 
    } 

    if (($method === 'PUT' || $method === 'POST') && isset($options['content'])) { 
     $http['content'] = $options['content']; 
     array_push($headers, 'Content-length: ' . strlen($options['content'])); 
     array_push($headers, 'Content-Type: application/json'); 
    } 

    $http['header'] = $headers; 

    $context = stream_context_create(array('http' => $http)); 

    var_dump(stream_context_get_options($context));echo('<hr/>'); 
    $fp = fopen($url, 'rb', false, $context); 
    if (! $fp) { 
     throw new \Exception('Request failed: $php_errormsg'); 
    } 
    $metadata = stream_get_meta_data($fp); 
    $content = stream_get_contents($fp); 
    $responseCode = (int)explode(' ', $metadata['wrapper_data'][0])[1]; 

    fclose($fp); 

    return array (
     'metadata' => $metadata, 
     'content' => $content, 
     'status' => $responseCode 
    ); 
    } 

ich jetzt alle Header in einer Array-Variablen und die Einstellung der Header-Eigenschaft auf, dass ich zu speichern.

Ich bin nicht 100% sicher, warum, aber ich denke, es hat etwas mit variablen Referenzen zu tun. In einer Iteration des Codes wurde die $ http Autorisierungseigenschaft mit einem Und-Zeichen von var_dump markiert und von fopen ignoriert. Eine weitere Iteration, die das kaufmännische Und-Zeichen entfernt hat.

+0

Siehe auch https://github.com/RusticiSoftware/TinCanPHP/pull/72#issuecomment-241751758 –