Solved

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

Posted on 2014-11-22
3
283 Views
Last Modified: 2014-12-07
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.
0
Comment
Question by:smksa
3 Comments
 
LVL 58

Expert Comment

by:Gary
Comment Utility
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
 
LVL 2

Author Comment

by:smksa
Comment Utility
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
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
Comment Utility
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

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

The purpose of this article is to demonstrate how we can use conditional statements using Python.
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now