Solved

PHP curl_getinfo array returning all data

Posted on 2014-04-11
10
2,014 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 110

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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
LVL 110

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 110

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 110

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 110

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
can i read my emails on lamp ftp 4 66
JSON decode 5 42
Horizontal Full Calendar using php 5 37
Load string Array from file 23 36
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

738 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