473,583 Members | 3,386 Online
Bytes | Software Development & Data Engineering Community
+ 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(TMPFILEP ATH, "/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(TMPFILEPA TH.'/curl_err.txt', 'w'); // error log file
curl_setopt($ch , CURLOPT_STDERR, $er); // log the errors

curl_setopt($ch , CURLOPT_AUTOREF ERER, true);
curl_setopt($ch , CURLOPT_FOLLOWL OCATION, true);
curl_setopt($ch , CURLOPT_RETURNT RANSFER, true);

curl_setopt($ch , CURLOPT_COOKIEJ AR, TMPFILEPATH.'/cboe_cookie.txt ');
curl_setopt($ch , CURLOPT_REFERER ,
'http://www.cboe.com/delayedQuote/QuoteTableDownl oad.aspx');
curl_setopt($ch , CURLOPT_URL,
'http://www.cboe.com/delayedQuote/QuoteTableDownl oad.aspx');
curl_setopt($ch , CURLOPT_USERAGE NT,
'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_MAXREDI RS, 1);
curl_setopt($ch , CURLOPT_POST, true); // enable HTTP POST
curl_setopt($ch , CURLOPT_POSTFIE LDS,
'__EVENTTARGET= '
.'&__EVENTARGUM ENT='
.'&__VIEWSTATE= '.urlencode('dD wtODQ5MjIyNjc7O z5rmegY+4O27l7u WcpGd4iU+1RpAA= =')
.'&ucHeader:ucC BOEHeaderLinks: ucCBOEHeaderSea rch:searchtext= '
.'&ucHeader:ucC BOEHeaderLinks: ucCBOEHeaderSea rch:Button1=Sea rch'
.'&ucQuoteTable DownloadCtl:txt Ticker='.$ticke rsymbol
.'&ucQuoteTable DownloadCtl:cmd Submit=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_COOKIEJ AR 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 2724

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

Similar topics

9
12579
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 programming of web pages and not just running "scripts" which have in-roads into service DLLs of other languages such as C#. Surely MS will put Curl...
5
20187
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) die( "Cannot allocate a new PHP-CURL handle\n" ); $fp = fopen("ttread.htm", "w"); curl_setopt($ch, CURLOPT_FILE, $fp);
4
12134
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 without the user having to wait. I'm doing this (with fsockopen) by sending an http request to a page that does the time consuming stuff and then...
2
1985
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 service is supposed to return). Funnily enough this usually happens when the service has not been used for a little while. Once it works, it works every...
4
12085
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 curl.php', the script works properly. However if I try it in a browser, nothing is returned. The code is...
4
5540
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 it run in the background. My host can't use exec nor flush the output to the browser. But, I can use curl and timeout.
3
3533
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 prompted to save the php-file, obviously nothing is executed. what i've checked: ..) curl in php works with a selfcompiled php5 binary executed via cgi...
3
10354
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: here: http://curl.haxx.se/libcurl/php/examples/cookiejar.html and here: http://curl.haxx.se/libcurl/php/examples/ebay_login.html
11
3713
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 the CLI: * About to connect() to api-3t.sandbox.paypal.com port 443 * Trying 216.113.191.88... * connected * Connected to...
0
7821
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8172
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
6577
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5370
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3814
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3841
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2328
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 we have to send another system
1
1424
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1152
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.