By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,704 Members | 1,122 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,704 IT Pros & Developers. It's quick & easy.

php/curl works from commandline, not browser??

P: n/a
While trying to learn the ins and outs of the php CURL library, I
decided to write a php script that posts a form on the Chicago Board
of Options (CBOE) web site, which returns an ASCII text file. CBOE
appears to keep your form query data in cookies, so this seemed like
a good use of curl.

Well, my script works just fine when run from the commandline.

When accessed from my browser, it returns an empty string where the
data should be. I have no idea why. Here is my script. Below it I
will add further comments.

========== begin script optionchain.php ==========
// cookie and error log path - SET THIS BEFORE TESTING
define(TMPFILEPATH, "/mf/home/unicorn/shell/tmp");

* This script gets option chain data in a comma-delimited text file
* from the Chicago Board of Options web site
* Example for Microsoft (MSFT) stock options:
* URL syntax:
* Commandline: % php optionchain.php MSFT

$tickersymbol = isset($_GET['ticker']) ? $_GET['ticker']
: $_SERVER['argv'][1]; // get argument from commandline if no $_GET

$ch = curl_init(); // initialize curl

curl_setopt($ch, CURLOPT_VERBOSE, true); // verbose errors
$er = fopen(TMPFILEPATH.'/curl_err.txt', 'w'); // error log file
curl_setopt($ch, CURLOPT_STDERR, $er); // log the errors

curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

curl_setopt($ch, CURLOPT_COOKIEJAR, TMPFILEPATH.'/cboe_cookie.txt');
curl_setopt($ch, CURLOPT_REFERER,
curl_setopt($ch, CURLOPT_URL,
curl_setopt($ch, CURLOPT_USERAGENT,
'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)');

// "log in" to web site by accessing first page (sets cookie)
$discard = curl_exec($ch);

// now post the form. The result will come after setting more
// cookies and receiving a redirect to a different URL,
// which returns data using the query data contained in a cookie.
// AFTER that we get redirected back to the original page,
// so limit redirects to 1

curl_setopt($ch, CURLOPT_MAXREDIRS, 1);
curl_setopt($ch, CURLOPT_POST, true); // enable HTTP POST
curl_setopt($ch, CURLOPT_POSTFIELDS,
.'&__VIEWSTATE='.urlencode('dDwtODQ5MjIyNjc7Oz5rme gY+4O27l7uWcpGd4iU+1RpAA==')
.'&ucHeader:ucCBOEHeaderLinks:ucCBOEHeaderSearch:s earchtext='
.'&ucHeader:ucCBOEHeaderLinks:ucCBOEHeaderSearch:B utton1=Search'
.'&ucQuoteTableDownloadCtl:txtTicker='.$tickersymb ol

$content = curl_exec($ch);

// Close resources
curl_close ($ch);

// display result
print "<pre>Data:\n{$content}\nEnd</pre>\n";
========== end script optionchain.php ==========

Now, my two files set in CURLOPT_COOKIEJAR and CURLOPT_STDERR are
world-writable, so there shouldn't be a problem there. Both files
contain information after running the script from the commandline.
The correct data is returned; commandline execution works fine.

However, after running the script from the browser, the cookiejar
is empty, and the error logfile has information suggesting that
cookies weren't dealt with in any way. I suspect this might be
why the browser is returning a null result, but why it would work
differently from the browser, I don't know. Any thoughts?

Sep 19 '06 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.