首页 文章

多个PHP cUrl发布到同一页面

提问于
浏览
4

所以要点是我需要将XML数据查询发布到网关页面以接收XML响应,O解析稍后,可以有3-60个查询到此Web服务,遗憾的是我现在必须运行一个简单的循环并一次做一个 . 在响应方面,我只需要响应中的1行(或最多5行),第2行是我需要包含图像数据的第一行 . 所以我希望有能力选择我正在阅读哪些行 .

我创建了一个简单的“读入”功能,就像我说的基本for循环一样,这里是我目前正在使用并想要修改的代码 .

$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);

限制:我不能使用?post = $ data0&post = $ data1&post = $ data3,所以我需要一个更好的解决方案 . 除此之外,我想看看这里可以做出哪些改进 .

2 回答

  • 0
  • 1

    由于快速反应的限制,

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

相关问题