Solved

PHP curl_getinfo array returning all data

Posted on 2014-04-11
10
1,806 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 82

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 108

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
 
LVL 108

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 108

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 108

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 82

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 108

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

Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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 …

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now