5

I am trying to replicate some Node JS into PHP but cannot seem to get it working! The node is below;

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

And my PHP which I cannot get working;

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

It is supposed to login into Sony’s servers and retrieve a OAuth code, the Node.js is working so it is possible but I cannot seem to get it working in PHP.

Any help would be much appreciated.

The CURL is working but I get an ?authentication_error=true when it should return a code which I can utilize.

6
  • is your content type correct? 'Content-Type: application/x-www-form-urlencoded' Commented Jun 11, 2014 at 18:00
  • Make sure you have turned on notices/warnings, so your vars we can't see (e.g. $userAgent) raise the alarm if they are accidentally not in scope. Commented Jun 11, 2014 at 18:26
  • Yes they are all in scope, as a side note the full node.js api can be found here github.com/jhewt/gumer-psn and even a PHP implementation of it can be found here, github.com/ilendemli/gumer-psn-php Commented Jun 11, 2014 at 18:32
  • I am just trying to get an understanding of it within my local environment Commented Jun 11, 2014 at 18:33
  • 3
    You have an error with your COOKIEJAR file. You are using an absolute path initially and missing the leading forward-slash in the latter usage. Commented Jun 16, 2014 at 10:17

2 Answers 2

2
+200

You are getting Authentication Error because, in the line

CURLOPT_COOKIEFILE => 'tmp/cookieJar`

You are using the wrong value for the cookieJar for CURL. You need to add .txt and correct the path to an absolute path like you've used earlier in your code. That's why CURL is throwing you an Authentication Error

Correcting that with the following should solve your problems.

CURLOPT_COOKIEFILE => '/tmp/cookieJar.txt`


Also change the following line

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

Like this:

var_dump(file_get_contents('/tmp/cookieJar.txt'), $res);
Sign up to request clarification or add additional context in comments.

Comments

1

As mentioned in the comments you used relative paths in two occasions and in the last usage you omitted the .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));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.