多个PHP cUrl发布到同一页面

编程入门 行业动态 更新时间:2024-10-10 08:22:08
本文介绍了多个PHP cUrl发布到同一页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

所以要点是,我需要发布XML数据查询到网关页面以接收一个XML响应,O解析以后,可以有从任何地方3-60查询到这个Web服务,我不幸的是必须运行一个简单的现在循环,一次做一个。在响应方面,我只需要响应中的1行(或最多5行),行2是我需要的包含图像数据的第一行。所以我想要能够选择我正在读的行,如果可能的话。

So the gist is that I need to post XML data query to a gateway page to receive a XML response which O parse later, there can be anywhere from 3-60 queries to this web service, I unfortunately have to run a simple loop right now and do them one at a time. On the response side, I will only need 1 (or a max of 5) of the lines in the response, line 2 is the first line that I need containing image data. So I'd like the ability to select which lines I am reading in if at all possible.

我创建了一个简单的读入功能,我说基本for循环,这里是我目前使用的代码,并希望修订。

I created a simple "Read in" function as I said out of a basic for loop, here's the code that I am currently using and would like to revise.

$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, 'example/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需要更好的解决方案。

Restrictions: I cannot use ?post=$data0&post=$data1&post=$data3 unfortunately, so I need a better solution. Other than that, I'd like to see what kinds of improvements can be made here.

推荐答案

由于快速反应的限制,

<?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' => 'ibudev.wvus/websvc/actions/wvsMessageRouter.php', 'readline' => 2, 'coptions' => NULL, 'content' => $data, 'story' => 'FULL', 'party' => NULL, ); m_curl($options); ?>

更多推荐

多个PHP cUrl发布到同一页面

本文发布于:2023-10-10 18:39:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1479400.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   页面   PHP   cUrl

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!