2010-12-06 21 views
3

Donc l'essentiel est que j'ai besoin de poster une requête de données XML à une page de passerelle pour recevoir une réponse XML qui O parser plus tard, il peut y avoir de 3 à 60 requêtes à ce service web, Je dois malheureusement exécuter une simple boucle maintenant et les faire un à la fois. Du côté de la réponse, je n'aurai besoin que de 1 (ou un maximum de 5) des lignes dans la réponse, la ligne 2 est la première ligne dont j'ai besoin pour contenir des données d'image. Je voudrais donc pouvoir sélectionner les lignes que je suis en train de lire si cela est possible.Messages PHP multiples cUrl à la même page

J'ai créé une simple fonction "Read in" comme je l'ai dit sur une base pour la boucle, voici le code que j'utilise actuellement et que j'aimerais réviser.

 

$part1 = 'XML Beginning'; $part2 = XML End'; 
$posts = array(0 => 'SC-010052214', 1 => 'SC-000032972', 2 => 'SC-012535460', 3 => 'SC-011257289', 4 => 'SC-010134078'); 


$ch = curl_init(); 

    curl_setopt($ch, CURLOPT_URL, 'http://example.com/index.php'); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER => 1); 
    curl_setopt ($ch, CURLOPT_POST, 1); 

$count = count($posts); 
for($i=0;$i<$count;$i++) { 

    curl_setopt ($ch, CURLOPT_POSTFIELDS, "payload=$part1{$posts[$i]}$part2"); 
    $return[] = curl_exec ($ch); 

    } 
curl_close ($ch); 

print_r($return); 

Restrictions: Je ne peux pas utiliser après = $ DATA0 & post = data1 $ & post = DATA3 $ malheureusement, donc je besoin d'une meilleure solution?. À part cela, j'aimerais voir quelles améliorations peuvent être apportées ici.

Répondre

0

En raison des limites en réponse rapide,

 
<?php 

function m_curl($input) { 

     // compile queries for usable locations 
     foreach($input['content'] as $pos=>$item) { 
      $query = '<childDetailQuery><request><query-replacement>'; 
      $query .= "<item_number>{$item}</item_number>"; 

        $query .= (isset($input['story']) && $input['story'] != NULL) 
           ? "<story_type>".$input['story']."</story_type>" 
           : '<story_type>SHORT</story_type>'; 

        $query .= (isset($input['party']) && $input['party'] != NULL) 
           ? "<party_number>".$input['party']."</party_number>" 
           : ''; 

      $query .= "</query-replacement><latency-tolerance>NONE</latency-tolerance>"; 
      $query .= '</request></childDetailQuery>'; 

     $queries[] = $query; 
       unset($query); 
     } 


     // make sure the rolling window isn't greater than the # of urls 
     $limit = 10; 
     $limit = (sizeof($queries) < $limit) ? sizeof($queries) : $limit; 

     $master = curl_multi_init(); 
     $curl_arr = array(); 

      // add additional curl options here 
      $std_options = array(
       CURLOPT_RETURNTRANSFER => 1, 
       CURLOPT_FOLLOWLOCATION => 1, 
       CURLOPT_MAXREDIRS => 0, 
      ); 

     $options = ($coptions) ? ($std_options + $coptions) : $std_options; 

     echo $input['location']; 
     // start the first batch of requests 
     for ($i = 0; $i < $limit; $i++) { 
      $ch = curl_init(); 

       $options[CURLOPT_POSTFIELDS] = "payload=".$queries[$i]; 

      curl_setopt_array($ch,$options); 
      curl_multi_add_handle($master, $ch); 
     } 

     do { 
      while(($execrun = curl_multi_exec($master, $running)) == CURLM_CALL_MULTI_PERFORM); 
       if($execrun != CURLM_OK) { 
            echo 'Curl Error'; break; 
       } 


      // a request was just completed -- find out which one 
      while($done = curl_multi_info_read($master)) { 
       $info = curl_getinfo($done['handle']); 
       if ($info['http_code'] == 200) { 
        $output = curl_multi_getcontent($done['handle']); 

        // request successful. process output using the callback function. 
        parse_returns($output); 

        // start a new request (it's important to do this before removing the old one) 
        $ch = curl_init(); 
        $options[CURLOPT_POSTFIELDS] = "payload=".$queries[$i++]; // increment i 
        curl_setopt_array($ch,$options); 
        curl_multi_add_handle($master, $ch); 

        // remove the curl handle that just completed 
        curl_multi_remove_handle($master, $done['handle']); 

       } else { 

        echo 'Failed on:'; var_dump($info); 
        echo 'With options:'; var_dump($options); 

        // request failed. add error handling. 
       } 
      } 
     } while ($running); 

     curl_multi_close($master); 
     return false; 
} 

function parse_returns($data) { 
    print_r($data); 
} 

// set query numbers 
$data = array(
    0 => 'SC-010052214', 
    1 => 'SC-000032972', 
    2 => 'SC-012535460', 
    3 => 'SC-011257289', 
    4 => 'SC-010134078' 
); 

// set options array 
$options = array(
    'location'  => 'http://ibudev.wvus.org/websvc/actions/wvsMessageRouter.php', 
    'readline'  => 2, 
    'coptions'  => NULL, 
    'content'  => $data, 
    'story'   => 'FULL', 
    'party'   => NULL, 
); 


m_curl($options); 

?>