Author

Topic: help me! php multi curl requests then echo with diff data format (Read 1598 times)

newbie
Activity: 10
Merit: 0
member
Activity: 110
Merit: 10
Here is a more simple case that may make it easier to see what is happening.

Code:
      require_once("getInfo.php");

$getInfo = new getInfo();

$infoObject $getInfo->getInfoObject();

echo("
");
print_r($infoObject);
echo("

"
);

echo("Difficulty = " $infoObject->difficulty "
"
);
echo("Estimate = " $infoObject->estimate "
"
);
echo("Hash Rate = " $infoObject->hashrate "
"
);
echo("Interval = " $infoObject->interval "
"
);
echo("Hashes to Win = " $infoObject->hashestowin "
"
);
?>



Code:

                        
class getInfo
                  {
  var $difficultyUrl "http://blockexplorer.com/q/getdifficulty";
var $estimateUrl   "http://blockexplorer.com/q/estimate";
var $hashrateUrl   "http://blockchain.info/q/hashrate";
var $intervalUrl   "http://blockexplorer.com/q/interval";
var $hashesUrl     "http://blockexplorer.com/q/hashestowin";
var $difficulty;
var $estimate;
var $hashrate;
var $interval;
var $hashestowin;
 
public function getInfo()
       {
                                        
$this->curl($this->difficultyUrl$result);
$this->difficulty $result;

$this->curl($this->estimateUrl$result);
$this->estimate $result;

$this->curl($this->hashrateUrl$result);
$this->hashrate $result;

$this->curl($this->intervalUrl$result);
$this->interval $result;

$this->curl($this->hashesUrl$result);
$this->hashestowin $result;
          } 

  public function curl($url, &$result)
  {
         $ch curl_init();
                                  
curl_setopt($chCURLOPT_URL$url);
  curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
  $result json_decode(curl_exec($ch));
  curl_close($ch);
  return 1;
  }

public function getInfoObject()
{
  return $this;
}
}
?>

newbie
Activity: 10
Merit: 0
Your php code to format a number to a decimal representation is correct.

Verify that the data that you are trying to format is what you actually think it is. In your sample code, you are treating the return responses from the curl calls as single values. I am trying to ascertain whether or not the curl calls are returning simply text, or if they are returning JSON. If they are returning JSON, then you cannot format it, and the number_format call is going to fail.

Get some real data, real code, and real results so someone can help. We cannot guess based on some examples that may or may not be what you are really doing.


sorry to bother you.
here a.php is real code. but b.php is no idea to me how can do that as I want it correctly.

Code:

$urls 
= array(
  
"http://blockexplorer.com/q/getdifficulty",
  
"http://blockexplorer.com/q/estimate",
  
"http://blockchain.info/q/hashrate",
  
"http://blockexplorer.com/q/interval",
  
"http://blockexplorer.com/q/hashestowin"
);

// Class to run parallel GET requests and return the transfer
class ParallelGet
{
  function 
__construct($urls)
  {
    
// Create get requests for each URL
    
$mh curl_multi_init();
    foreach(
$urls as $i => $url)
    {
      
$ch[$i] = curl_init($url);
      
curl_setopt($bhash4win_CurlCURLOPT_HEADER0);
  curl_setopt($ch[$i], CURLOPT_RETURNTRANSFER1);
      
curl_multi_add_handle($mh$ch[$i]);
    }

    
// Start performing the request
    
do {
        
$execReturnValue curl_multi_exec($mh$runningHandles);
    } while (
$execReturnValue == CURLM_CALL_MULTI_PERFORM);
    
// Loop and continue processing the request
    
while ($runningHandles && $execReturnValue == CURLM_OK) {
      
// Wait forever for network
      
$numberReady curl_multi_select($mh);
      if (
$numberReady != -1) {
        
// Pull in any new data, or at least handle timeouts
        
do {
          
$execReturnValue curl_multi_exec($mh$runningHandles);
        } while (
$execReturnValue == CURLM_CALL_MULTI_PERFORM);
      }
    }

    
// Check for any errors
    
if ($execReturnValue != CURLM_OK) {
      
trigger_error("Curl multi read error $execReturnValue\n"E_USER_WARNING);
    }

    
// Extract the content
    
foreach($urls as $i => $url)
    {
      
// Check for errors
      
$curlError curl_error($ch[$i]);
      if(
$curlError == "") {
        
$res[$i] = curl_multi_getcontent($ch[$i]);
echo $res[$i]."\n";

      } else {
        print 
$i."error to get a result"."\n";
      }
  
      
// Remove and close the handle
      
curl_multi_remove_handle($mh$ch[$i]);
      
curl_close($ch[$i]);
    }
    
// Clean up the curl_multi handle


    
curl_multi_close($mh);
    }


}
?>


then the result are below

8974296.0148879
9959823.99753607
71824.46261642574
525
527
38544896037004515


I just want to echo them from diff php file (b.php to include a.php)
besides modify the value format of easy result.
member
Activity: 110
Merit: 10
Your php code to format a number to a decimal representation is correct.

Verify that the data that you are trying to format is what you actually think it is. In your sample code, you are treating the return responses from the curl calls as single values. I am trying to ascertain whether or not the curl calls are returning simply text, or if they are returning JSON. If they are returning JSON, then you cannot format it, and the number_format call is going to fail.

Get some real data, real code, and real results so someone can help. We cannot guess based on some examples that may or may not be what you are really doing.
newbie
Activity: 10
Merit: 0
I think you are not handling the object correctly. Can you do something? Add this bit of code right before you echo out the results in b.php:
Code:
echo("
");
print_r($res);
echo("

");

I don't think your input is what you think it is, but if you can show me what that looks like, I may have a little better understanding of what is happening. I am a little confused though because you are showing 5 lines of output, yet your code only accounts for 3.

first, ignore the json array that I've mentioned above.
else if all results are just numerics that I want to show them directly.
how can assign number_format on each result ?

the b.php was just an example like I meant to show it from include.

Code:
include('a.php');
echo("
");
print_r($res);


here the results are come from one command line (echo $res;).
I need to get outputs one by one so that modify the each value format.

823.45566558758   // number_format(res(1), 2)
0.0222222235854   // number_format(res(2), 4)
1.43354455    // number_format(res(3), 3)
855554566654.52211545648484   // number_format(intval(res(4),3)
member
Activity: 110
Merit: 10
I think you are not handling the object correctly. Can you do something? Add this bit of code right before you echo out the results in b.php:
Code:
echo("
");
print_r($res);
echo("

");

I don't think your input is what you think it is, but if you can show me what that looks like, I may have a little better understanding of what is happening. I am a little confused though because you are showing 5 lines of output, yet your code only accounts for 3.
newbie
Activity: 10
Merit: 0
well, one is json array others is just the result itself to use directly.

when I've finished to attach them on html it shows like below

823.45566558 // (e.g. need to show it with decimal 2 digits format)
0.022222223 // (4 digits)
1.43354455  // (2 digits)
"data":{"high":{"value":"135.89899","value_int":"13589899","display":"$135.90","display_short":"$135.90","currency":"USD"}, //  (need value_int)
855554566654.52211545648484 // (int)

how can parse each result with different format on html ?

Very Thx in advance.
member
Activity: 110
Merit: 10
I think we would be happy to help; It is unclear to me what you are expecting to happen, and what is really happening. Obviously you are trying to display some data of some sort, but I can't tell what format that data is in. Do you know? Is it JSON, or XML. I guess I can't see that you are instantiating your class either.

Do you see anything for output at all?
newbie
Activity: 10
Merit: 0
hey guys
fist I'm totally noob to develop script and html function

however, I've succeded to echo get_contents with parallel multi curl requests.
but the echo outputs need to show them with diff data format like one is number formats with two digit, other int.
how can show to get the correct data on array result?

anyone can help me this simple code?


here is a.php file for multi curls as shown below

Code:

// Define the URLs
$urls = array(
  
"http://example.com/1/",
  
"http://example.com/2/",
  
"http://example.com/3/",
  
"http://example2.com/4/",
  
"http://example3.com/5/",
);

// Class to run parallel GET requests and return the transfer
class ParallelGet
{
  function 
__construct($urls)
  {
    
// Create get requests for each URL
    
$mh curl_multi_init();
    foreach(
$urls as $i => $url)
    {
      
$ch[$i] = curl_init($url);
      
curl_setopt($ch[$i], CURLOPT_HEADER0);
      
curl_setopt($ch[$i], CURLOPT_RETURNTRANSFER1);
      
curl_multi_add_handle($mh$ch[$i]);
    }

    
// Start performing the request
    
do {
        
$execReturnValue curl_multi_exec($mh$runningHandles);
    } while (
$execReturnValue == CURLM_CALL_MULTI_PERFORM);
    
// Loop and continue processing the request
    
while ($runningHandles && $execReturnValue == CURLM_OK) {
      
// Wait forever for network
      
$numberReady curl_multi_select($mh);
      if (
$numberReady != -1) {
        
// Pull in any new data, or at least handle timeouts
        
do {
          
$execReturnValue curl_multi_exec($mh$runningHandles);
        } while (
$execReturnValue == CURLM_CALL_MULTI_PERFORM);
      }
    }

    
// Check for any errors
    
if ($execReturnValue != CURLM_OK) {
      
trigger_error("Curl multi read error $execReturnValue\n"E_USER_WARNING);
    }

    
// Extract the content
    
foreach($urls as $i => $url)
    {
      
// Check for errors
      
$curlError curl_error($ch[$i]);
      if(
$curlError == "") {
        
$res[$i] = curl_multi_getcontent($ch[$i]);
echo $res[$i]."\n";
      } 
      else {
        print 
$i."error to get result"."\n";
      }
  
      
// Remove and close the handle
      
curl_multi_remove_handle($mh$ch[$i]);
      
curl_close($ch[$i]);
    }

    
// Clean up the curl_multi handle


    
curl_multi_close($mh);
    }
    
// Print the response data


}
?>


here b.php to include a.php above


b.php


Code:
include('a.php');
//* here below the exact place I want to show them with diff data format. but totally no idea cuz I'm noob on scripts.[/color][/b]*//
//* just guess need another foreach and define to get each $res(i) *//
echo number_format($res[1],2)."\n"
echo 
number_format($res[2],0)."\n";
echo 
ceil(number_format($res[3],1))."\n";
?>



I've been googling for 3 days to solve this shaby problem.
totally no idea.
help me please
Jump to: