0

How to run url from php script in the same way (exactly the same behaviour) as in browser when i run url from address bar. I mean with the same header data, cookies and additional data which browser send. How to add this data in php.

I need this cause when I logged in, answers from this 2 cases are not the same:

  1. in browser I still logged in and this is correct
  2. from php run I am logged OUT - not correct

I've tried file_get_contents nad curl (from here) but it doesn't work properly - response is still different.

I'm calling http://127.0.0.1/check.html and here is function check:

public function check(){
    echo 'begin';
    //      $total_rows = file_get_contents('https://127.0.0.1:8443/example.html?shopId=121');
    $total_rows = $this->getUrl('https://127.0.0.1:8443/example.html', '121');
    print_r($total_rows);
    echo 'end';
}

function getUrl($url, $shopId ='') {
    $post = 'shopId=' . $shopId;

    $ch = curl_init();

    $cookie_string="";
    foreach( $_COOKIE as $key => $value ) {
      $cookie_string .= "$key=$value;";
    };
    $cookie_string .= "JSESSIONIDSSO=66025D1CC9EF39ED7F5DB024B6026C61";

//        echo $cookie_string;;

    $ch = curl_init();
    curl_setopt($ch,CURLOPT_COOKIE, $cookie_string);
    //      curl_setopt($ch, CURLOPT_PORT, 8443);  
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
    //      curl_setopt ($ch, CURLOPT_CAINFO, dirname(__FILE__)."/../../files/cacert.pem");
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);     
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
    //        curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Secure Content-Type: text/html; charset=UTF-8")); 
    //        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Host: 127.0.0.1:8443'));

    $ret = curl_exec($ch);
    curl_error ($ch );
    curl_close($ch);
    return $ret;
}
Community
  • 1
  • 1
studentttt
  • 61
  • 9
  • 1
    Are you passing the correct headers & session/cookie values and any other parameters while using any of the methods described above. To get a correct solution, please post any code that you might have tried. Just stating "This doesn't work" does not warrant a fulsome response. `What have you tried`? – web-nomad Aug 27 '12 at 12:34
  • don't know how to add 'correct headers & session/cookie values' from browser to php – studentttt Aug 27 '12 at 13:58
  • Okay, you have used `CURLOPT_HEADER` in the example above and set it to 0, which is akin to sending no headers.Also, there is a curl variable for cookies. Please check the php manualand examples which would give you an idea on how this works. That might help you. – web-nomad Aug 27 '12 at 14:11
  • Thx for answer! But... still dosen't work, even with cookies: I've added to code: CURLOPT_COOKIE – studentttt Aug 27 '12 at 14:35
  • If you want to replicate the request made from the browser, then first capture the request actually being sent from the browser. Then you can set the appropriate headers. If this is your own app that you're making the request to, then it should be even easier to figure out how the requests are different or why you get different responses. Lastly, you talk about being logged in and logged out, but there's nothing in the code that would indicate any logging in or out. – Lèse majesté Aug 27 '12 at 14:46
  • OK, when I add 'JSESSIONIDSSO' to '$cookie_string' there is behave properly! But now have another problem: how can I get this parameter from php code (retrive from browser)? – studentttt Aug 27 '12 at 15:09

2 Answers2

0

Try it: http://www.lastcraft.com/browser_documentation.php Or that: http://sourceforge.net/projects/snoopy/ Or that: php curl: how can i emulate a get request exactly like a web browser? Hope help

Community
  • 1
  • 1
Guerra
  • 2,792
  • 1
  • 22
  • 32
0

You can execute the cron job using your PHP script to execute the other script.

Florent
  • 12,310
  • 10
  • 49
  • 58
G10DRA
  • 61
  • 1
  • 1
  • 6