我有一些代碼,它會請求從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的版本相同的結果。我得到的迴應是:
{
"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
}
通過使用https://requestb.in我可以看到PHP7版本不包括Authorization頭
什麼是PHP 5.6.10和PHP 7之間改變導致此?我如何解決它?
編輯:刪除一些紅色鯡魚文本並添加請求Bin結果。
對於初學者,我在PHP5請求中看到''WWW-Authenticate:Basic realm = \「Jive SBS \」「'而不是PHP7。我從來沒有使用這個API,所以也許你應該與他們聯繫。有可能PHP7發送不同的頭文件,他們的API認爲這是一種黑客攻擊嘗試? – MonkeyZeus
謝謝。這給了我一個想法。我會嘗試在我控制的端點處針對請求,並看看有什麼不同。 –
NP,另一個需要考慮的事情是使用CURL而不是fopen(),因爲它看起來好像只是與JSON API交互,而fopen()只是處理URL時的CURL封裝。我懷疑使用fopen()會有什麼好處,但我不是那麼專業的好運氣! – MonkeyZeus