Curl Multi Init 从已执行页面回显内容
Curl Multi Init Echoing Content from executed pages
我遇到了 Curl Multi Init 的问题。我试图让它同时访问多个站点,然后将它们的内容保存到变量中。不幸的是,我不明白为什么它似乎在呼应每个站点的内容。例如,如果我告诉它去两个站点,一个站点的内容是 "hello",另一个是 "hey",它会回显 "hellohey"——不确定为什么会这样。这是我使用的代码:
<?php
// create both cURL resources
$ch1 = curl_init();
$ch2 = curl_init();
// set URL and other appropriate options
curl_setopt($ch1, CURLOPT_URL, "http://example1.org/");
curl_setopt($ch1, CURLOPT_HEADER, 0);
curl_setopt($ch2, CURLOPT_URL, "http://example2.org/");
curl_setopt($ch2, CURLOPT_HEADER, 0);
//create the multiple cURL handle
$mh = curl_multi_init();
//add the two handles
curl_multi_add_handle($mh,$ch1);
curl_multi_add_handle($mh,$ch2);
$active = null;
//execute the handles
do {
$mrc = curl_multi_exec($mh, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);
while ($active && $mrc == CURLM_OK) {
if (curl_multi_select($mh) == -1) {
usleep(100);
}
do {
$mrc = curl_multi_exec($mh, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);
}
//close the handles
curl_multi_remove_handle($mh, $ch1);
curl_multi_remove_handle($mh, $ch2);
curl_multi_close($mh);
?>
好的。您没有将 CURLOPT_RETURNTRANSFER
设置为 true
:return 作为字符串传输而不传递给 stdout
(curl-setopt#CURLOPT_RETURNTRANSFER)。变化:
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, 1 );
要将它们的内容保存到变量中,您应该使用curl-multi-getcontent。
我遇到了 Curl Multi Init 的问题。我试图让它同时访问多个站点,然后将它们的内容保存到变量中。不幸的是,我不明白为什么它似乎在呼应每个站点的内容。例如,如果我告诉它去两个站点,一个站点的内容是 "hello",另一个是 "hey",它会回显 "hellohey"——不确定为什么会这样。这是我使用的代码:
<?php
// create both cURL resources
$ch1 = curl_init();
$ch2 = curl_init();
// set URL and other appropriate options
curl_setopt($ch1, CURLOPT_URL, "http://example1.org/");
curl_setopt($ch1, CURLOPT_HEADER, 0);
curl_setopt($ch2, CURLOPT_URL, "http://example2.org/");
curl_setopt($ch2, CURLOPT_HEADER, 0);
//create the multiple cURL handle
$mh = curl_multi_init();
//add the two handles
curl_multi_add_handle($mh,$ch1);
curl_multi_add_handle($mh,$ch2);
$active = null;
//execute the handles
do {
$mrc = curl_multi_exec($mh, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);
while ($active && $mrc == CURLM_OK) {
if (curl_multi_select($mh) == -1) {
usleep(100);
}
do {
$mrc = curl_multi_exec($mh, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);
}
//close the handles
curl_multi_remove_handle($mh, $ch1);
curl_multi_remove_handle($mh, $ch2);
curl_multi_close($mh);
?>
好的。您没有将 CURLOPT_RETURNTRANSFER
设置为 true
:return 作为字符串传输而不传递给 stdout
(curl-setopt#CURLOPT_RETURNTRANSFER)。变化:
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, 1 );
要将它们的内容保存到变量中,您应该使用curl-multi-getcontent。