2014-06-11 11 views
5

Tôi đang cố gắng sao chép một số JS Mã vào PHP nhưng dường như không thể làm cho nó hoạt động! Nút dưới đây;Node JS to PHP, không thể làm cho nó hoạt động

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

Và PHP của tôi mà tôi không thể làm việc;

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

Giả định đăng nhập vào máy chủ của Sony và truy xuất mã OAuth, Node.js đang hoạt động để có thể nhưng tôi dường như không làm cho nó hoạt động trong PHP.

Mọi trợ giúp sẽ được đánh giá cao.

CURL đang hoạt động nhưng tôi nhận được ?authentication_error=true khi cần trả lại mã mà tôi có thể sử dụng.

+0

là kiểu nội dung của bạn có đúng không? 'Loại nội dung: ứng dụng/x-www-form-urlencoded' – Dan

+0

Đảm bảo bạn đã bật thông báo/cảnh báo, vì vậy các vars của chúng tôi không thể thấy (ví dụ: '$ userAgent') nâng cao báo thức nếu chúng vô tình không trong phạm vi. – halfer

+0

Có tất cả chúng nằm trong phạm vi, như một lưu ý phụ api node.js đầy đủ có thể được tìm thấy ở đây https://github.com/jhewt/gumer-psn và thậm chí cả việc thực hiện PHP của nó có thể được tìm thấy ở đây, https://github.com/ilendemli/gumer-psn-php – user2251919

Trả lời

2

Bạn đang nhận được Authentication Error bởi vì, trong dòng

CURLOPT_COOKIEFILE => 'tmp/cookieJar` 

Bạn đang sử dụng sai giá trị cho cookieJar cho CURL. Bạn cần thêm .txt và sửa đường dẫn đến đường dẫn tuyệt đối như bạn đã sử dụng trước đó trong mã của mình. Đó là lý do tại sao CURL ném bạn một số Authentication Error

Việc khắc phục vấn đề sau đây sẽ khắc phục được sự cố của bạn.

CURLOPT_COOKIEFILE => '/tmp/cookieJar.txt` 


Cũng thay đổi dòng sau

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

Như thế này:

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

Như đã đề cập trong các ý kiến ​​bạn sử dụng đường dẫn tương đối trong hai lần và trong việc sử dụng cuối cùng bạn bỏ qua .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)); 
Các vấn đề liên quan