hosted by liquidweb


Go Back   Web Hosting Talk : Web Hosting Main Forums : Programming Discussion : PHP: Run URL and capture output in a variable
Reply

Forum Jump

PHP: Run URL and capture output in a variable

Reply Post New Thread In Programming Discussion Subscription
 
Send news tip View All Posts Thread Tools Search this Thread Display Modes
  #1  
Old
New Member
 
Join Date: May 2004
Posts: 3
Question

PHP: Run URL and capture output in a variable


What PHP function can I use to RUN a URL and then capture the output in a URL??

I can't use fopen() or any of the file opening functions because a file is not returned, only a short string.

This short string is displayed in my browser if I request the URL with my browser. I can't find any PHP functions that can read the output of a URL if it is not a file.

This is such a basic thing to want to do.

How would you do it??

Thanks a lot,
Paul



Sponsored Links
  #2  
Old
Web Hosting Guru
 
Join Date: May 2004
Posts: 305
so you just want to get the contents of a url into a string?


file_get_contents()

http://www.php.net/file_get_contents

  #3  
Old
Community Guide
 
Join Date: Jul 2003
Location: Kuwait
Posts: 5,099
Most of the file opening functions have URL wrappers that allow you to open URLs.

Some depend on php.ini settings (allow_url, if I remember correctly). The safest way to do this sort of things is to use any number of HTTP classes (like snoopy) or cURL if you have that functionality compiled in.

__________________
In order to understand recursion, one must first understand recursion.
If you feel like it, you can read my blog
Signal > Noise

Sponsored Links
  #4  
Old
Newbie
 
Join Date: Apr 2004
Posts: 19
try to open URL like: $result_string = join('', file('http://google.com'));

__________________
From Adult Web Hosting Service with love

  #5  
Old
Web Hosting Guru
 
Join Date: May 2004
Posts: 305
the easiest and as far as I know about the only way to do this using a function in PHP is

$string = file_get_contents("http://www.example.com/file.php");

  #6  
Old
New Member
 
Join Date: May 2004
Posts: 3
file_get_contents

Thanks Flush. For some reason I had over-looked this function but in any case it's no good because the server my site is on is running PHP 4.1.2.

I can't find anyway to get 4.1.2 to do the job and fear you may be right that file_get_contents() is the only way to do this.

But because it seems a relatively simple task for PHP to perform, I can't help doubting that it took until version 4.3 to accomodate this.

Can someone with PHPv4.3+ check that this works:
PHP Code:
$url="https://select.worldpay.com/wcc/itransaction";
$urloutput=file_get_contents($url);
echo 
$urloutput//should print "N,-1,Cart id must be provided" 
If so, I'll badger my host for a php upgrade.

Thanks again,
Paul

  #7  
Old
Newbie
 
Join Date: Jun 2002
Location: Kansas City
Posts: 12
i believe you will need cURL if your going to be communicating with secure pages over https://

__________________
Email : ben.summers@gmail.com | AIM : beeeeeeeeeeeeeen | MSN : ben@bensummers.net

  #8  
Old
Newbie
 
Join Date: Apr 2004
Posts: 19
Quote:
Can someone with PHPv4.3+ check that this works:

PHP:
$url="https://select.worldpay.com/wcc/itransaction";
$urloutput=file_get_contents($url);
echo $urloutput; //should print "N,-1,Cart id must be provided"
I have PHP version 4.3.3 and your script return string:
N,-1,Could not derive installation ID

__________________
From Adult Web Hosting Service with love

  #9  
Old
New Member
 
Join Date: May 2004
Posts: 3
Smile Solution Found!

Thanks Adult4Web. That means that function would work.

Anyhow, for my 4.1.2 version: I've taken the advice of fyrestrtr and BenSummers and used curl (which surprisingly my host had installed). This works:

PHP Code:
$url="https://select.worldpay.com/wcc/itransaction?etc";
$ch curl_init();   
curl_setopt($chCURLOPT_URL,$url); 
curl_setopt($chCURLOPT_RETURNTRANSFER,1); // return into a variable 
curl_setopt($chCURLOPT_TIMEOUT10); 
$result curl_exec($ch); 
curl_close($ch); 
Thank you all. This is a pretty class forum...
Paul

Reply

Related posts from TheWhir.com
Title Type Date Posted
Cloudmeter Survey Shows Most IT Solutions Fail to Capture Critical Customer Data Web Hosting News 2013-08-29 12:24:20
Strategies to Reduce Operating Costs, Avoid Email Blacklisting and Improve Deliverability Webinars 2014-06-10 11:05:29
The Value of Partnerships Part 1: Leveraging an Existing Customer Base Blog 2014-04-24 13:27:37
Lead Generation Part 4 – Go it Alone Blog 2014-08-14 15:20:55
Parallels Summit 2013 Web Hosting Events 2013-01-31 19:51:16


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes
Postbit Selector

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump
Login:
Log in with your username and password
Username:
Password:



Forgot Password?
WHT Host Brief Email:

We respect your privacy. We will never sell, rent, or give away your address to any outside party, ever.

Advertisement:
Web Hosting News:
WHT Membership
WHT Membership



 

X

Welcome to WebHostingTalk.com

Create your username to jump into the discussion!

WebHostingTalk.com is the largest, most influentual web hosting community on the Internet. Join us by filling in the form below.


(4 digit year)

Already a member?