Bitcoin Forum
December 15, 2024, 03:26:35 AM *
News: Latest Bitcoin Core release: 28.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: help me! php multi curl requests then echo with diff data format  (Read 1602 times)
EddieYann (OP)
Newbie
*
Offline Offline

Activity: 10
Merit: 0


View Profile
April 28, 2013, 12:22:50 PM
 #1

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:
<?php

// 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]."<br />\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

<div class="resFormat">
Code:
<?php 
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)."<br />\n"
echo 
number_format($res[2],0)."<br />\n";
echo 
ceil(number_format($res[3],1))."<br />\n";
?>

</div>

I've been googling for 3 days to solve this shaby problem.
totally no idea.
help me please
minzie
Member
**
Offline Offline

Activity: 110
Merit: 10



View Profile WWW
April 28, 2013, 12:46:20 PM
 #2

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?

Professional Freelance Web Apps Engineer ° Portfolio °  ModoBot Automated Trading Platform ° BitThingy ° My Github  ° Skype: cclites_1
EddieYann (OP)
Newbie
*
Offline Offline

Activity: 10
Merit: 0


View Profile
April 28, 2013, 01:11:43 PM
Last edit: April 28, 2013, 01:26:53 PM by EddieYann
 #3

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.
minzie
Member
**
Offline Offline

Activity: 110
Merit: 10



View Profile WWW
April 28, 2013, 02:15:58 PM
 #4

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("<pre>");
print_r($res);
echo("</pre><br>");

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.

Professional Freelance Web Apps Engineer ° Portfolio °  ModoBot Automated Trading Platform ° BitThingy ° My Github  ° Skype: cclites_1
EddieYann (OP)
Newbie
*
Offline Offline

Activity: 10
Merit: 0


View Profile
April 28, 2013, 02:40:07 PM
Last edit: April 28, 2013, 03:32:47 PM by EddieYann
 #5

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("<pre>");
print_r($res);
echo("</pre><br>");

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("<pre>");
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)
minzie
Member
**
Offline Offline

Activity: 110
Merit: 10



View Profile WWW
April 28, 2013, 04:01:20 PM
 #6

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.

Professional Freelance Web Apps Engineer ° Portfolio °  ModoBot Automated Trading Platform ° BitThingy ° My Github  ° Skype: cclites_1
EddieYann (OP)
Newbie
*
Offline Offline

Activity: 10
Merit: 0


View Profile
April 28, 2013, 04:24:53 PM
 #7

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:
<?php

$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]."<br />\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.
minzie
Member
**
Offline Offline

Activity: 110
Merit: 10



View Profile WWW
April 28, 2013, 06:50:14 PM
 #8

Here is a more simple case that may make it easier to see what is happening.

Code:
<?php
      
require_once("getInfo.php");

$getInfo = new getInfo();

$infoObject $getInfo->getInfoObject();

echo("<pre>");
print_r($infoObject);
echo("</pre><br>");

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



Code:
<?php

                        
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;
}
}
?>


Professional Freelance Web Apps Engineer ° Portfolio °  ModoBot Automated Trading Platform ° BitThingy ° My Github  ° Skype: cclites_1
EddieYann (OP)
Newbie
*
Offline Offline

Activity: 10
Merit: 0


View Profile
April 28, 2013, 08:11:35 PM
 #9

Here is a more simple case that may make it easier to see what is happening.

Code:
<?php
      
require_once("getInfo.php");

$getInfo = new getInfo();

$infoObject $getInfo->getInfoObject();

echo("<pre>");
print_r($infoObject);
echo("</pre><br>");

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



Code:
<?php

                        
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;
}
}
?>


damn you are man.
Awesome. it works perfect..
plz check My PM. I want to pay something to u...
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!