Jump to content

Run cURL commands without printing HTML output on the page?


doncoglioni

Recommended Posts

Hey everyone :)

 

I'm using cURL with PHP to automate some tasks online, basically logging into a website and going through various pages.  I don't want to see any output on the screen when I run my .php script, basically I just need to "fetch" stuff and save it to disk.  It functions perfectly, but every page I "visit" with cURL gets printed to my web browser, and it's very annoying! 

 

So is there a way to run cURL WITHOUT printing the source of the page it's fetching?  I'll post my code, it's probably a silly mistake:

 

	$ch=curl_init(); 
curl_setopt($ch, CURLOPT_URL, "https:\\www.secure.site.com\login.php");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); 
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
curl_setopt($ch, CURLOPT_MAXREDIRS, 4);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,0); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, 'username=user&password=secret'); 
curl_setopt($ch, CURLOPT_TIMEOUT, 200); 
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.1.9) Gecko/20071025 Firefox/2.0.0.9'); 
curl_setopt($ch, CURLOPT_REFERER, 'https:\\www.secure.site.com\index.php');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0); 
$buffer = curl_exec($ch);
curl_close($ch);

 

Thanks, everyone!

Link to comment
Share on other sites

PFMaBiSmAd is right.  Let me just elaborate on it a little bit so that you (and/or) anyone else can learn form this.  Based upon my previous posts with you you will understand that I'm all about over explaining things and making posts as clear as possible (AND posting all code ;-) ) so that someone else can learn form everything.  Here is the boring explain...

 

By default when you execute curl it will simply just print everything to the screen.  However you can use the following command to prevent that...

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

 

That basically says "Change the setting of the return transfer of curl and instead store it in a variable."  (as apposed to showing it in the browser)

 

Normally when you want to execute curl all that you need to do is this..

curl_exec($ch);  //this will execute curl and print to screen (assuming you did NOT set CURLOPT_RETURNTRANSFER to 1)

 

BUT if you have CURLOPT_RETURNTRANSFER set to 1 then you need to do this..

$buffer = curl_exec($ch);

 

The above will then execute curl NOT print anything AND just STORE all of your output in the variable of $buffer.

 

IF you then want to print everything to the screen you would then do this..

<?php
  echo $buffer;
?>

 

OR you could use a series of string searching functions to extract data.  For example the following would pull out the TITLE of the page...

 

<?php
    ######### Search html code for the needed string #########
//find the location of the market value
$response_start = strPos($buffer, "<title>");
$response_end = strPos($buffer, "</title>", $response_start);
$temp_code = substr($buffer, $response_start + 7, ($response_end - $response_start - 7));
?>

 

Here are the 3 lines of code above in plain english..

1. Find the first occurrence  of the word "<title>" in the html code

2. Find the first occurrence of the word "</title>" AFTER the first occurrence of the word "<title>" in the html code

3. Make a substring that is between the words "<title>" and "</title>" in your html code.  In this particular case if you ran curl on THIS very posting on php freaks it would return the following...

 

"Run cURL commands without printing HTML output on the page?"

 

Hope that helped out =)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.