473,480 Members | 2,277 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

php/curl works from commandline, not browser??

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 ==========
<?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 www.cboe.com.
*
* Example for Microsoft (MSFT) stock options:
*
* URL syntax: http://example.com/optionchain.php?ticker=MSFT
*
* 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,
'http://www.cboe.com/delayedQuote/QuoteTableDownload.aspx');
curl_setopt($ch, CURLOPT_URL,
'http://www.cboe.com/delayedQuote/QuoteTableDownload.aspx');
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,
// http://www.cboe.com/delayedQuote/QuoteData.dat
// 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,
'__EVENTTARGET='
.'&__EVENTARGUMENT='
.'&__VIEWSTATE='.urlencode('dDwtODQ5MjIyNjc7Oz5rme gY+4O27l7uWcpGd4iU+1RpAA==')
.'&ucHeader:ucCBOEHeaderLinks:ucCBOEHeaderSearch:s earchtext='
.'&ucHeader:ucCBOEHeaderLinks:ucCBOEHeaderSearch:B utton1=Search'
.'&ucQuoteTableDownloadCtl:txtTicker='.$tickersymb ol
.'&ucQuoteTableDownloadCtl:cmdSubmit=Download');

// Get data (RETURNS NULL FROM BROWSER, WORKS FROM COMMANDLINE ??)
$content = curl_exec($ch);

// Close resources
curl_close ($ch);
fclose($er);

// 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?

-A
Sep 19 '06 #1
0 2699

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
12556
by: Conrad F | last post by:
Hi, If any Microsoft people are listening.... Are there any plans for the new web language called "Curl" to be supported in .NET (ASP.NET)? I ask as Curl represents the first step to true OO...
5
20149
by: Basta | last post by:
I'm trying to retrieve information of a website using PHP and Curl. This is the code I use: <? $tturl = "http://teletekst.nos.nl/"; echo "opening $tturl ...\n"; $ch = curl_init(); if (! $ch)...
4
12117
by: yawnmoth | last post by:
Is it possible to send http requests with curl but not have curl wait for the response? The reason I ask is because I'd like to code a web app that can sorta start time consuming processes...
2
1963
by: Iain Adams | last post by:
Hey I am using cURL to connect to a web service. The web service returns a result. This works most the time however some times curl doesnt return anything, no errors and no xml (what the web...
4
12019
by: BinnyVA | last post by:
Hi, I am using PHP 5.1.2 with curl enabled. But whenever I try to use curl to fetch a url, it fails - 'curl_exec()' returns nothing. But if I try to execute the same file in CLI - like 'php...
4
5517
by: Terry | last post by:
I'm using curl to invoke a php script on the same site/server. It works great, but if I call it again while it's still running, nothing happens. Why? Can that be fixed? Why use curl? To make...
3
3523
by: Matthias Leopold | last post by:
hi i've got problems getting php4 scripts which use curl to work on RHEL4 (Nahant Update 4) and CentOS 4.4. when the script is accessed the browser "hangs", after a couple of minutes i'm...
3
10347
by: JDS | last post by:
So, I'd like to create the following scenario: 1) Use cURL library within PHP (cURL + "Cookie Jar", et.al) to create a virtual browser session that "logs in" to a remote site. (For example: ...
11
3704
by: Flexor | last post by:
I have a php script that runs from command line and makes an https request to paypal, using curl. It works fine if I run it from a web page. It fails if I run it from CLI. The error I get from...
0
7048
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6911
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7050
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7091
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5344
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4488
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2988
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1303
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
564
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.