我正在尝试将一些Node JS复制到PHP中,但似乎无法使其正常工作!节点在下面;
function initLogin(callback) {
debug('Getting login');
request.get({
url: psnURL.SignIN
, headers : {
'User-Agent': 'Mozilla/5.0 (Linux; U; Android 4.3; '+options.npLanguage+'; C6502 Build/10.4.1.B.0.101) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30 PlayStation App/1.60.5/'+options.npLanguage+'/'+options.npLanguage
}
}
, function (error, response, body) {
(typeof callback === "function" ? getLogin(callback, psnVars.SENBaseURL + response.req.path) : getLogin(undefined, psnVars.SENBaseURL + response.req.path));
})
}
/*
* @desc Login into PSN/SEN and creates a session with an auth code
* @param Function callback - Calls this function once the login is complete
*/
function getLogin(callback, url) {
request.post(psnURL.SignINPOST
,{
headers: {
'Origin':'https://auth.api.sonyentertainmentnetwork.com'
,'Referer': url
}
,form:{
'params' : 'service_entity=psn'
,'j_username' : options.email
,'j_password' : options.password
}
}, function (error, response, body) {
request.get(response.headers.location, function (error, response, body) {
if (!error) {
var urlString = unescape(response.req.path);
psnVars.authCode = urlString.substr(urlString.indexOf("authCode=") + 9, 6);
debug('authCode obtained: ' + psnVars.authCode);
getAccessToken(psnVars.authCode, callback);
}
else {
debug('ERROR: ' + error)
}
})
}
)
}
我的PHP无法正常工作;
$c = curl_init();
curl_setopt_array($c, array(
CURLOPT_URL => $PSNSignIn,
CURLOPT_USERAGENT => $userAgent,
CURLOPT_HEADER => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => 2,
CURLOPT_COOKIEJAR => realpath('/tmp/cookieJar.txt'),
CURLOPT_FAILONERROR => 1,
));
$res = curl_exec($c);
$path = explode($SENBaseURL, curl_getinfo($c, CURLINFO_EFFECTIVE_URL));
$referer = $SENBaseURL . $path[1];
var_dump(file_get_contents('tmp/cookieJar.txt'), $res);
$c = curl_init();
curl_setopt_array($c, array(
CURLOPT_URL => $SignINPOST,
CURLOPT_USERAGENT => $userAgent,
CURLOPT_REFERER => $referer,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => 2,
CURLOPT_HEADER => array(
'Origin: https://auth.api.sonyentertainmentnetwork.com',
'Content-Type: application/x-www-form-urlencoded',
),
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => array(
'params' => 'service_entity=psn',
'j_username' => $username,
'j_password' => $password,
),
CURLOPT_COOKIEFILE => 'tmp/cookieJar',
CURLOPT_FAILONERROR => 1,
));
$res = curl_exec($c);
var_dump($res, curl_getinfo($c));
它应该登录到索尼的服务器并检索OAuth代码,Node.js正在运行,所以它可能,但我似乎无法让它在PHP中工作。
非常感谢任何帮助。
CURL正在运行但我得到一个?authentication_error=true
,它应该返回一个我可以使用的代码。
答案 0 :(得分:2)
您正在获取Authentication Error
,因为在
CURLOPT_COOKIEFILE => 'tmp/cookieJar`
cookieJar
CURL
使用了错误的值。您需要添加.txt
并将路径更正为您之前在代码中使用过的绝对路径。这就是为什么CURL
会给你一个Authentication Error
通过以下方式进行纠正可以解决您的问题。
CURLOPT_COOKIEFILE => '/tmp/cookieJar.txt`
同时更改以下行
var_dump(file_get_contents('tmp/cookieJar.txt'), $res);
喜欢这样:
var_dump(file_get_contents('/tmp/cookieJar.txt'), $res);
答案 1 :(得分:1)
正如评论中所提到的,您曾两次使用相对路径,而在上一次使用中,您省略了.txt
:
$c = curl_init();
curl_setopt_array($c, array(
CURLOPT_URL => $PSNSignIn,
CURLOPT_USERAGENT => $userAgent,
CURLOPT_HEADER => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => 2,
CURLOPT_COOKIEJAR => realpath('/tmp/cookieJar.txt'),
CURLOPT_FAILONERROR => 1,
));
$res = curl_exec($c);
$path = explode($SENBaseURL, curl_getinfo($c, CURLINFO_EFFECTIVE_URL));
$referer = $SENBaseURL . $path[1];
var_dump(file_get_contents('/tmp/cookieJar.txt'), $res);
$c = curl_init();
curl_setopt_array($c, array(
CURLOPT_URL => $SignINPOST,
CURLOPT_USERAGENT => $userAgent,
CURLOPT_REFERER => $referer,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => 2,
CURLOPT_HEADER => array(
'Origin: https://auth.api.sonyentertainmentnetwork.com',
'Content-Type: application/x-www-form-urlencoded',
),
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => array(
'params' => 'service_entity=psn',
'j_username' => $username,
'j_password' => $password,
),
CURLOPT_COOKIEFILE => '/tmp/cookieJar.txt',
CURLOPT_FAILONERROR => 1,
));
$res = curl_exec($c);
var_dump($res, curl_getinfo($c));