How to run PHP script with session check via linux shell ?

Hi,

I would like to request an assistant.

I have the following script :


function getClient() {

    $client = new SoapClient('http://url:8080/SKS1A/SKS1A?wsdl');

    // is soapcookies already set? (must be already logged in)

    if (isset($_SESSION['soapcookies'])) 
          {

        // just set the cookies
        foreach ($_SESSION['soapcookies'] as $cookiename => $value)
            $client->__setCookie($cookiename, $value[0]);
          } 
    else  {

        //please change the username and password

        $result = $client->login(array('username' => $username, 'password' => $password))->return;
        //print_r($result);

        //store the soap cookies in $_SESSION so that don't need to reauthenticate.
        $_SESSION['soapcookies'] = $client->_cookies;
          }

    return $client;
}

$query = "SELECT id,domainname from domain WHERE status='check'";
$data = mysql_query($query, $conn) or die(); 

echo "Cron Check Start<br>";

	while($rec = mysql_fetch_array($data))
	{ 
		
//get the SOAP client connection

$api = getClient();

$domain = $rec[domainname]; 
$arg0 = $reseller;
$arg1 = $domain; //sample result available

$result = $api->checkDomainAvailability(array('arg0' =>$arg0,'arg1' =>$arg1))->return;  
$xml = new SimpleXMLElement($result);
$result = $xml->code; 
$description = $xml->reason;

			if($result == 1000) { echo "Domain $domain successfully checked<br>";		}
					
            else { echo "Domain: '".$rec[domainname]."' '".$description."' ERROR<br>"; }

	}// end while.

Open in new window



May i know how to passed the session check in the script so that i can run the script finely via shell as if using browser ?

The purpose of doing this is that i want to set a cronjob to run this script.

A few complete sample with my current code inside it is appreciated.

Thank you.
LVL 2
Shamsul KamalJunior TechAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

GaryCommented:
Sessions don't really exist in a CLI, they are a browser implementation referenced by a cookie that tells the server who you are and what session belongs to you
0
Shamsul KamalJunior TechAuthor Commented:
Can curl help in this case ?

Can curl pretend to be a browser using curl and execute the script ?

Anybody can give me some sample ?
0
Ray PaseurCommented:
cURL, running a PHP script on a server, can simulate a client browser with pretty good fidelity.  In order for the client browser to appear "well-behaved" it must accept and return cookies.  I believe this script will do that part of it, but I have not tested it past PHP 5.3.

<?php // demo/curl_get_cookies.php
error_reporting(E_ALL);


// DEMONSTRATE THE BASICS OF CURL
// SOMETHING LIKE RAY_curl_get_cookies.php?url=http://twitter.com


// YOU COULD HAVE SOMETHING LIKE THIS
$url = isset($_GET["url"]) ? $_GET["url"] : 'http://twitter.com';

// BUT SINCE IT IS ON MY SERVER, I HAVE HARD-CODED THIS
$url = 'http://twitter.com';

// TRY THE REMOTE WEB SERVICE
$htm = my_curl($url);
$dat = file_get_contents('cookie.txt');

// SHOW THE WORK PRODUCT OR BARK OUT ERROR MESSAGES
echo "<pre>";
echo PHP_EOL . "<b>$url</b>";
echo PHP_EOL . "<i>$dat</i>";
echo PHP_EOL;

// ACTIVATE THIS TO SEE THE HTML STRING
// echo PHP_EOL . htmlentities($htm);


// A FUNCTION TO RUN A CURL-GET CLIENT CALL TO A FOREIGN SERVER
function my_curl
( $url
, $timeout=3
, $error_report=TRUE
)
{
    $curl = curl_init();

    // HEADERS AND OPTIONS APPEAR TO BE A FIREFOX BROWSER REFERRED BY GOOGLE
    $header[] = "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
    $header[] = "Cache-Control: max-age=0";
    $header[] = "Connection: keep-alive";
    $header[] = "Keep-Alive: 300";
    $header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
    $header[] = "Accept-Language: en-us,en;q=0.5";
    $header[] = "Pragma: "; // BROWSERS USUALLY LEAVE BLANK

    // SET THE CURL OPTIONS - SEE http://php.net/manual/en/function.curl-setopt.php
    curl_setopt( $curl, CURLOPT_URL,            $url  );
    curl_setopt( $curl, CURLOPT_USERAGENT,      'Mozilla/5.0 (Windows NT 6.1; rv:22.0) Gecko/20100101 Firefox/22.0'  );
    curl_setopt( $curl, CURLOPT_HTTPHEADER,     $header  );
    curl_setopt( $curl, CURLOPT_REFERER,        'http://www.google.com'  );
    curl_setopt( $curl, CURLOPT_ENCODING,       'gzip,deflate'  );
    curl_setopt( $curl, CURLOPT_AUTOREFERER,    TRUE  );
    curl_setopt( $curl, CURLOPT_RETURNTRANSFER, TRUE  );
    curl_setopt( $curl, CURLOPT_FOLLOWLOCATION, TRUE  );
    curl_setopt( $curl, CURLOPT_COOKIEFILE,     'cookie.txt' );
    curl_setopt( $curl, CURLOPT_COOKIEJAR,      'cookie.txt' );
    curl_setopt( $curl, CURLOPT_TIMEOUT,        $timeout  );

    // RUN THE CURL REQUEST AND GET THE RESULTS
    $htm = curl_exec($curl);

    // ON FAILURE HANDLE ERROR MESSAGE
    if ($htm === FALSE)
    {
        if ($error_report)
        {
            $err = curl_errno($curl);
            $inf = curl_getinfo($curl);
            echo "CURL FAIL: $url TIMEOUT=$timeout, CURL_ERRNO=$err";
            var_dump($inf);
        }
        curl_close($curl);
        return FALSE;
    }

    // ON SUCCESS RETURN XML / HTML STRING
    curl_close($curl);
    return $htm;
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.