2014-06-11 12 views
5

Ich versuche, einige Node JS in PHP zu replizieren, aber es scheint nicht zu funktionieren! Der Knoten ist unten;Knoten JS zu PHP, kann es nicht funktionieren

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

Und meine PHP, die ich nicht arbeiten kann;

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

Es soll in Sonys Server einzuloggen und einen OAuth-Code abrufen, arbeitet die Node.js so ist es möglich, aber ich kann nicht scheinen, um es in PHP arbeiten.

Jede Hilfe würde sehr geschätzt werden.

Die CURL funktioniert, aber ich bekomme eine ?authentication_error=true, wenn es einen Code zurückgeben soll, den ich verwenden kann.

+0

ist Ihr Inhaltstyp korrekt? 'Content-Type: application/x-www-form-urlencoded' – Dan

+0

Stellen Sie sicher, dass Sie Hinweise/Warnungen aktiviert haben, so dass Ihre Vars, die wir nicht sehen können (zB $ userAgent), den Alarm auslösen im Visier. – halfer

+0

Ja, sie sind alle im Umfang, als eine Randnotiz die vollständige node.js api finden Sie hier https://github.com/jhewt/gumer-psn und sogar eine PHP-Implementierung davon finden Sie hier, https://github.com/ilendemli/gumer-psn-php – user2251919

Antwort

2

Du Authentication Error bekommen, weil in der Linie

CURLOPT_COOKIEFILE => 'tmp/cookieJar` 

Sie verwenden den falschen Wert für cookieJar für CURL. Sie müssen .txt hinzufügen und den Pfad zu einem absoluten Pfad korrigieren, den Sie zuvor in Ihrem Code verwendet haben. Deshalb wirft CURL einen Authentication Error

Korrektur, die mit den folgenden sollte Ihre Probleme lösen.

CURLOPT_COOKIEFILE => '/tmp/cookieJar.txt` 


Auch ändern Sie die folgende Zeile

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

So:

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

Wie in den Kommentaren erwähnt Sie relative Pfade in zwei Gelegenheiten und in der letzten Benutzung verwendet ausgelassen Sie die .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));