简体   繁体   中英

How to set cookie on external domain using PhantomJS

Hello I want to ask if is it possible to set cookie on external domain using PhantomJS? I tried below code..

var webPage = require('webpage');
var page = webPage.create();
page.settings.javascriptEnabled = true;
page.settings.userAgent = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36';


phantom.addCookie({
  'name'     : 'ps_idssss',   /* required property */
  'value'    : '123123',  /* required property */
  'domain'   : 'casperjs.org',
  'path'     : '/',                /* required property */
  'expires'  : (new Date()).getTime() + (1000 * 60 * 60)   /* <-- expires in 1 hour */
});

page.open('http://casperjs.org', function (status) {

  console.log(page.cookies[0].name);
  phantom.exit();
});

I executed it using PHP

<?php

$output = shell_exec('phantomjs cookie-js --cookies-file=~/phantomjs/cookies.txt');


echo $output           //outputs ps_idssss

?>

IT successfully output the cookie but when I visits the http://casperjs.org , it never saved any cookie. Please help

It won't work because of the wrong order of arguments. Script name should be the last argument.

$output = shell_exec('phantomjs --cookies-file=~/phantomjs/cookies.txt cookie-js');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM