Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

multi cURL with classes

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

multi cURL with classes

Postby AZDJedi » Mon Jun 02, 2014 11:34 am

Hey,

I'm having difficulty conceptualizing and coding the following scenario. I'm really close but I need that last step...hopefully you can help.

The old method I was using (which worked fine) was 2 classes that each had a cURL request. This was slow so I made a single class that was inherited by the 2 child classes.

Each of the child classes would call parent::runCurl()

In the parent Exchange class I put the following code which I modified from searching Google and reading the help files:

Code: Select all
    public function __construct() {
   $this->ch = curl_init();
   curl_multi_add_handle($_SESSION['curl_mh'],$this->ch);
//$_SESSION['curl_mh'] is initialized during login.
    }
   private function runCurl() {
      $this->full_curl_multi_exec($_SESSION['curl_mh'], $this->curl_active);
      
      while ($this->curl_active && $mrc == CURLM_OK) {
         //PHP bug, so run this
         if (curl_multi_select($_SESSION['curl_mh']) != -1) {
            usleep(100);
         }
         
         while(($info = curl_multi_info_read($_SESSION['curl_mh']))) {
echo $info["result"];
            if ($info["result"] == CURLE_OK){
               $this->content = curl_multi_getcontent($info["handle"]);
echo "here";
               print_r($this->content);
            }
         }
         
         $this->full_curl_multi_exec($_SESSION['curl_mh'], $this->curl_active);
      }            
   }
   
   function full_curl_multi_exec($mh, &$still_running) {
      do {
         $rv = curl_multi_exec($mh, $still_running);
      } while ($rv == CURLM_CALL_MULTI_PERFORM);       
      return $rv;
   }


The result of $info['result'] is 3. I only get it once and no "here" is ever seen.

Does anybody know what's up? Have I even conceptualized this properly? I need it to fetch two requests and process the data when they're both done.

P.S. I should add that each child class has different CURL options and sends to a different url.
AZDJedi
New php-forum User
New php-forum User
 
Posts: 6
Joined: Fri Mar 16, 2012 9:20 am

Re: multi cURL with classes

Postby AZDJedi » Tue Jun 03, 2014 10:11 am

I figured it out. I needed to run curl_init() during each run, not in the __construct.

My next problem is returning the actual data. The data comes in, and I can echo it during the function...but it doesn't get returned to the calling function. Very weird.
AZDJedi
New php-forum User
New php-forum User
 
Posts: 6
Joined: Fri Mar 16, 2012 9:20 am


Return to PHP coding => General

Who is online

Users browsing this forum: Google [Bot], Google Feedfetcher and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.