略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: curl_multi_getcontent

2024-03-28

curl_multi_getcontent

(PHP 5, PHP 7, PHP 8)

curl_multi_getcontent如果设置了CURLOPT_RETURNTRANSFER,则返回获取的输出的文本流

说明

curl_multi_getcontent(resource $ch): string

如果CURLOPT_RETURNTRANSFER作为一个选项被设置到一个具体的句柄,那么这个函数将会以字符串的形式返回那个cURL句柄获取的内容。

参数

handle

curl_init() 返回的 cURL 句柄。

返回值

如果设置了CURLOPT_RETURNTRANSFER,则返回获取的输出的文本流。

参见

add a noteadd a note

User Contributed Notes 3 notes

up
20
michael at xendica dot com
8 years ago
This seems to work as expected for me - allowing me to get the content from a curl_multi operation into variables :

(Thanks go to many other notes in related documentation (there is much copy/pasting) all I did was add the relevant line(s))

<?
    $aURLs = array("http://www.php.net","http://www.w3cschools.com"); // array of URLs
    $mh = curl_multi_init(); // init the curl Multi
   
    $aCurlHandles = array(); // create an array for the individual curl handles

    foreach ($aURLs as $id=>$url) { //add the handles for each url
        $ch = curl_setup($url,$socks5_proxy,$usernamepass);
        $ch = curl_init(); // init curl, and then setup your options
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // returns the result - very important
        curl_setopt($ch, CURLOPT_HEADER, 0); // no headers in the output

        $aCurlHandles[$url] = $ch;
        curl_multi_add_handle($mh,$ch);
    }
   
    $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) {
            do {
                $mrc = curl_multi_exec($mh, $active);
            } while ($mrc == CURLM_CALL_MULTI_PERFORM);
        }
    }
   
/* This is the relevant bit */
        // iterate through the handles and get your content
    foreach ($aCurlHandles as $url=>$ch) {
        $html = curl_multi_getcontent($ch); // get the content
                // do what you want with the HTML
        curl_multi_remove_handle($mh, $ch); // remove the handle (assuming  you are done with it);
    }
/* End of the relevant bit */

    curl_multi_close($mh); // close the curl multi handler

?>
up
0
butesa at freenet dot de
8 years ago
You can use curl_multi_getcontent() on a curl handle that was executed with curl_exec() (and not added to a multi handle).
However, this is not very useful because curl_multi_getcontent() will return the same as curl_exec() then.

<?php
$ch
= curl_init('http://www.example.com/');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$a = curl_exec($ch);
$b = curl_multi_getcontent($ch);
var_dump($a === $b);
curl_close($ch);
?>
will return:

bool(true)
up
-5
nolife
4 years ago
It's probably a bug in the curl multi implementation:

In many many cases you keep the curl connection alive after a curl_exec().
With curl_multi that's not possible if you don't do this after every get_content() :
curl_multi_remove_handle($ch);
curl_multi_add_handle($ch);

I've not yet tested if this also breaks the keepalive connection but if you don't do this you'll just get the old previous response after each curl_multi_exec()

官方地址:https://www.php.net/manual/en/function.curl-multi-getcontent.php

冷却塔厂家 广告
-- 广告
北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3