Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

PHP curl_getinfo array returning all data

Posted on 2014-04-11
10
Medium Priority
?
2,196 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 84

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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 84

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

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

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…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

636 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