2016-08-22 3 views
0

Jive의 API에서 데이터를 가져 오도록 요청하는 코드가 있습니다. MAMP에서이 코드를 실행하면 PHP 5.6.10 또는 PHP 7.0.0 중 하나를 실행할 수 있습니다. PHP5를 사용하면 성공적인 응답을 얻을 수 있습니다. PHP 7에서는 401 Unauthorized를 얻습니다. fopen no PHP 7의 인증 헤더

관련 기능

은 여기에 있습니다 :

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

위해서 var_dump 호출이 모두 PHP 버전과 동일한 결과를 생성합니다. 내가 얻을 응답은 다음과 같습니다

나는 PHP7 버전이 원인이 PHP 5.6.10과 PHP (7) 사이에 변경된 무엇 인증 헤더

를 포함하지 않는 것을 볼 수 있습니다 https://requestb.in를 사용하여

{ 
    "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 
} 

? 어떻게 수정해야합니까?

편집 : 빨간색 청어 텍스트를 제거하고 요청 빈 결과를 추가하십시오.

+0

우선 WWW-Authenticate : 기본 영역 = "Jive SBS \" "'를 PHP5 요청에서는 볼 수 있지만 PHP7에서는 볼 수 없습니다. 나는이 API로 일한 적이 없기 때문에 아마도 그들과 연락을해야 할 것이다. PHP7에서 해킹 시도라고 생각하는 API가 다른 헤더를 보낼 수 있습니다. – MonkeyZeus

+0

감사합니다. 그것은 저에게 아이디어를줍니다. 내가 제어하는 ​​엔드 포인트에서 요청을 대상으로 시도하고 다른 점을 확인합니다. –

+0

NP, 고려해야 할 또 하나의 것은 단순히 JSON API와 상호 작용하는 것처럼 보이고 URL을 처리 할 때 fopen()이 CURL의 래퍼 일 뿐이므로 fopen() 대신 CURL을 사용하는 것입니다. 나는 fopen()을 사용하는 데 전혀 도움이되지 않는다는 것을 의심 스럽지만 전문가에게는 그렇게 좋은 행운이 아니다! – MonkeyZeus

답변

0

다음 작품 : 이제 배열 변수에 모든 헤더를 저장하고 그것과 헤더 속성을 설정하고있어

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

.

저는 100 % 확실하지는 않지만 왜 변수 참조와 관련이 있다고 생각합니다. 코드의 한 반복에서 $ http 권한 부여 속성은 var_dump에 의해 앰퍼샌드로 표시되었고 이는 fopen에 의해 무시되었습니다. 앰퍼샌드를 제거한 또 다른 반복 작업.

+0

https://github.com/RusticiSoftware/TinCanPHP/pull/72#issuecomment-241751758도 참조하십시오. –