Solved

PHP curl_getinfo array returning all data

Posted on 2014-04-11
10
1,892 Views
Last Modified: 2014-05-13
I am using CURL in PHP to download a file over HTTP to collect statistics on the download. I am following Example #1 on the manual page (http://www.php.net/manual/en/function.curl-getinfo.php) but when I echo $info['total_time'] it outputs all the downloaded data. How can I echo that variable without having the contents of the downloaded file displayed?
0
Comment
Question by:ITNC
  • 5
  • 3
  • 2
10 Comments
 
LVL 83

Accepted Solution

by:
Dave Baldwin earned 500 total points
ID: 39995552
Add curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); between curl_init and curl_exec.  

http://www.php.net/manual/en/function.curl-setopt.php
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 40003806
I would like to see the code example that shows the excess output.  This sounds like a bug.  It should not work that way according to the PHP man page, which tells us that the return values consists of the value of opt if given, or an associative array with the array keys and data elements that correspond to opt, or FALSE on failure.

Please post the code that shows us how to reproduce the failure, thanks. ~Ray
0
 

Author Comment

by:ITNC
ID: 40003828
Ray, the code was taken from that man page exactly. I fixed this by dumping the data to a txt file before the echo of the array values.
0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
LVL 109

Expert Comment

by:Ray Paseur
ID: 40004006
code was taken from that man page exactly
That's why I asked to see the code so I can reproduce the issue.  There is a lot of latitude in what you might mean by "exactly" since there may be environmental variables in play.  The issue may not even be in the code, but without seeing it, we are just guessing.

When I copy the example, I get this:
http://www.iconoun.com/demo/temp_itnc.php

<?php // demo/temp_itnc.php
error_reporting(E_ALL);

// Create a curl handle
$ch = curl_init('http://www.yahoo.com/');

// Execute
curl_exec($ch);

// Check if any error occurred
if(!curl_errno($ch))
{
 $info = curl_getinfo($ch);

 echo 'Took ' . $info['total_time'] . ' seconds to send a request to ' . $info['url'];
}

// Close handle
curl_close($ch);
?>

Open in new window

0
 

Author Comment

by:ITNC
ID: 40004034
I had made several modifications but here was the original code I was using:

<?php
$ch = curl_init($p_value);

curl_setopt($ch);
curl_exec($ch);
if(!curl_errno($ch))
{
 $info = curl_getinfo($ch);
      echo "<p>Size Download: " . $info['size_download'] . " bytes";
}
curl_close($ch);

?>

The $p_value variable is just a URL pointing to a text document.
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 40004062
Using this code I get a warning about curl_setopt() and I also get the correct value for $info['size_download'] in the message.  What am I missing?

<?php // demo/temp_itnc.php
error_reporting(E_ALL);

$p_value = 'http://www.yahoo.com/';

$ch = curl_init($p_value);

curl_setopt($ch);
curl_exec($ch);
if(!curl_errno($ch))
{
    $info = curl_getinfo($ch);
    echo "<p>Size Download: " . $info['size_download'] . " bytes";
}
curl_close($ch);

Open in new window

0
 

Author Comment

by:ITNC
ID: 40004079
I would get the correct value as well but only after it printed the entire fetched document. I don't know why it was doing that but after using fopen to save the retrieved text to a file it would no longer print the fetched document and only show the text I specified.
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 40004169
I don't know why it was doing that (print the entire fetched document)
If you can show us the code that causes this phenomenon, we can probably help.  But without seeing the code, all we can do is guess and life is too short to waste your time guessing.
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 40004442
This version includes the line I suggested in the first comment and it does not display the page when it is run, only the info requested.
<?php // demo/temp_itnc.php
error_reporting(E_ALL);

$p_value = 'http://www.yahoo.com/';

$ch = curl_init($p_value);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_exec($ch);
if(!curl_errno($ch))
{
    $info = curl_getinfo($ch);
    echo "<p>Size Download: " . $info['size_download'] . " bytes";
}
curl_close($ch);

Open in new window

0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 40004470
@Dave: That's essentially the same thing I see, except without RETURNTRANSFER there is an "error" message.  I put "error" in quotes because it's not written to the error_log, just sent to the browser.  The message is generated from within curl_exec();  The $info variable contains exactly what one would expect from curl_getinfo().
0

Featured Post

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question