How can I call curl from a Perl CGI script? - perl

I have a CGI (perl) script that is attempting to call curl using the open command:
#curl = ('/usr/bin/curl', '-S','-v','--location', $url,
'-H', 'Content-Type:'.$content_type,
'-H', "Authorization: $authorization",
'-H', "X-Gdata-Key:$gdata_key",
'-H', "Content-Length:$content_length",
'-H','GData-Version:2',
'--data',"\#$filename");
And then executed like so:
open CURL, "-|", #curl;
The program works perfectly from the command line, but when I try to run it in the browser, the page ends up timing out.
What do I need to change on my server or in my script to get this to work properly?

You should check if the open succeeded and also attempt to close the pipe explicitly, check for error. In case of error, die with the error message. Then find the error message in the server error log.

In addition to the Sinan's suggestion, the timeout you are getting might point to a long running process - always an issue when run under CGI. Please look at other solutions like a Queue Manager. I use Beanstalk for these situations. But I have heard good things about Gearman and The Schwartz
I also learnt a lot about running processes that take a lot of time under CGI from this article

After looking at the error log and seeing the error
[Mon Nov 30 14:59:07 2009] [error] slurp_filename(
'/var/www/vhosts/mydomain.net/httpdocs /youtube/youtube.pl') / opening: (2)
No such file or directory at /usr/lib64/perl5/vendor_perl/5.8.6/
x86_64-linux-thread-multi/ModPerl/RegistryCooker.pm line 540
I thought it had something to do with the fact that I'm passing in my XML to curl as a file instead of as a string. Here is the new command that works with the xml passed as a string instead:
#curl = ('/usr/bin/curl', '-S','-v','--location', $url, '-H',
'Content-Type:'.$content_type,'-H',"Authorization: $authorization",'-H',
"X-Gdata-Key:$gdata_key",'-H',"Content-Length:$content_length",'-H',
'GData-Version:2','--data',"$xml");
And I am still using the command below to open/call curl:
open CURL, "-|", #curl;
It now runs successfully in the browser and returns me the values I am requesting with it.

Related

Run Perl script directly from GitHub

I am trying to run a Perl script directly from GitHub. This thread seems to address my issue (indeed, it helped me run dofiles in Stata directly from GitHub). However, when I type the following in a command prompt:
"perl https://rawgit.com/EconJoe/medline2014-xmlparsers/master/desc2014_meshtreenumbers.pl"
I get The following error message:
"Can't open perl script "https://rawgit.com/EconJoe/medline2014-xmlparsers/master/desc2014_meshtreenumbers.pl": Invalid argument"
Thanks for any help.
perl can't fetch a script from a URL. You have to do that separately.
curl -L https://rawgit.com/EconJoe/medline2014-xmlparsers/master/desc2014_meshtreenumbers.pl | perl

FastCgiModule error 500 with Azure

I am trying to run a Perl Fast CGI process in Azure. I have built a hello world test page (test.pl) and if I run it from the command it works
d:\home\site\wwwroot\bin\perl\bin\perl.exe -MFCGI::IIS=do test.pl
I have a handler for *.pl to run d:\home\site\wwwroot\bin\perl\bin\perl.exe (Strawbery Perl) with the arguments -MFCGI::IIS=do (i.e. the same as the above).
I get a 500 error. When I view the detailed log the only information I get back is FastCgiModule Error Code 0x8007000d.
My file runs from the command line but it won't run within the web app. Why not?
The problem was simple. My perl hello world worked from the command line, but for a web page it obviously needs to set the content type. I was missing the first line!
print "Content-type: text/html", "\n\n";
print "Hello World.\n";

LWP::Simple getstore does not work in Windows

As a part of my Perl script I have the following simple command which is supposed to download a $url and store its contents to a file $file:
getstore("$url", "$file");
This works perfectly fine when I run it on my fedora Linux, however when I run the same code on Windows, it seems that the $file remains empty..
This is very strange since, as I mentioned, there was no change to the code and this command is very basic.
Maybe anyone has a clue why such behavior might occur?
Thanks!
Finding a proxy quite easy sometimes:
Try to open a nonexisting webpage and most of time the PROXY reports an error and print out its own details.
Try this: -.ds.com
Check the line: Your web proxy is:....

Perl Location on Mac OSX

I'm beginning to write a simple Perl program on my Mac, and I understand that the first line needs to be the location of Perl itself, every example or tutorial I find tells me the first line should be:
"#!/usr/bin/perl"
However, with that there, I attempt to run the file under localhost and I get this error:
Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator, you#example.com and inform them of the time the error occurred, and anything you might have done that may have caused the error.
More information about this error may be available in the server error log.
Anyone have any idea why this is happening?
Thanks in advance, and let me know if any more information is needed!
P.S. if it helps, when I execute the command: "perl -v" it tells me
This is perl, v5.10.0 built for darwin-thread-multi-2level
(with 2 registered patches, see perl -V for more detail)
As Erik said, /usr/bin/perl is the standard location for Perl on OSX. You can also verify this by running which -a perl from terminal (this will list all instances of Perl on your path).
Can you run your script from the command-line, i.e. ./<myscript>.pl? It's possible that you haven't made the script executable.
#!/usr/bin/perl is the correct path for 10.6. If you're running from the webserver, your first line before any output should be a HTML header. You may have forgotten one?
#!/usr/bin/perl
use CGI;
print CGI->header('text/html');
print "hello world";

Why can't Win32::OLE talk to Excel2003 under Win2003?

I have a Web-based Perl Win32::OLE script that uses Excel on the server side. It has been working happily for years on a Win2000 server running Excel2000. We recently upgraded to Win2003/Excel2003 and I now get the following error from the script:
Win32::OLE(0.1709) error 0x80070005: "Access is denied" at create_worksheet_lib.plx line 639
eval {...} called at create_worksheet_lib.plx line 639
line 639 is:
$Excel = Win32::OLE->new('Excel.Application', sub {$_[0]->Quit;}) or die "Oops, cannot start Excel";
It appears that Perl no longer has access to Excel. Any Windows gurus out there that might be able to help trouble shoot this?
Seems like the same wicked problem described in another post, however, no solution found...
Maybe troubleshooting with Procmon will reveal where the problem lies (see http://support.microsoft.com/kb/286198).
Can you run the script from a command line on the server (taking the web server out of the mix)? That's the first thing I'd try. If it works from the command line then it's probably a permissions issue or a web server configuration issue.