2014-06-11 19 views
5

Bazı Node JS'yi PHP'ye kopyalamaya çalışıyorum ama çalışmayı başaramıyorum! Düğüm aşağıdadır;Node JS to PHP, çalışmadı

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

Ve benim çalışamayan PHP'm;

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

Bir OAuth kodu Sony'nin sunucularına içine giriş ve almak gerekiyordu, node.js mümkündür böylece çalışıyor ama PHP çalışan almak için görünmüyor olabilir.

Herhangi bir yardım çok takdir edilecektir.

CURL çalışıyor, ancak kullanabileceğim bir kod döndüğünde ?authentication_error=true aldım. Eğer iki kez göreli yolları kullanılan yorumlarda ve son kullanım belirtildiği gibi

+0

mı? 'Content-Type: application/x-www-form-urlencoded' – Dan

+0

Bildirimleri/uyarıları açtığınızdan emin olun, böylece göremediğiniz varsınız (ör. "$ UserAgent") yanlışlıkla değilse kapsamında. – halfer

+0

Evet hepsi bir arada, bir nota olarak tam node.js api burada bulunabilir https://github.com/jhewt/gumer-psn ve hatta bir PHP uygulaması burada bulunabilir, https://github.com/ilendemli/gumer-psn-php – user2251919

cevap

2

, çünkü doğrultusunda

CURLOPT_COOKIEFILE => 'tmp/cookieJar` 

Sen CURL için cookieJar için yanlış değeri kullanıyor. .txt'u eklemeniz ve kodunuzda daha önce kullandığınız gibi mutlak bir yolun yolunu düzeltmeniz gerekir. Bu nedenle, CURL'un size bir Authentication Error

'u atması buradaki sorunların giderilmesini düzeltir.

CURLOPT_COOKIEFILE => '/tmp/cookieJar.txt` 


Ayrıca değişim aşağıdaki satırı Bunun gibi

var_dump(file_get_contents('tmp/cookieJar.txt'), $res); 

: içerik türü doğru

var_dump(file_get_contents('/tmp/cookieJar.txt'), $res); 
1

atladığımız .txt: Sen Authentication Error alıyorsanız

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