?
Solved

PHP curl_getinfo array returning all data

Posted on 2014-04-11
10
Medium Priority
?
2,090 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 2000 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 111

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
WordPress Tutorial 3: Plugins, Themes, and Widgets

The three most common changes you will make to your website involve the look (themes), the functionality (plugins), and modular elements (widgets).

In this article we will briefly define each again, and give you directions on how to install them.

 
LVL 111

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 111

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 111

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 111

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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 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…
Suggested Courses

770 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