什么是add_url_multi_handle()?

时间:2017-06-26 20:32:16

标签: php api curl

我在任何文档中找不到它,当我搜索它时它不会出现。我正在尝试关注此tutorial,这是我目前的代码:

$location_urls = array();

foreach($entity_ids as $url) {

    array_push($location_urls, 'https://something.com/api/v1/location/' . $url);

}

$mh = curl_multi_init();

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

    add_url_to_multi_handle($mh, $location_urls);

}

这是我得到的错误:

<b>Fatal error</b>:  Uncaught Error: Call to undefined function add_url_to_multi_handle() in C:\xampp\htdocs\api_test\example.php:45
Stack trace:
#0 {main}
thrown in <b>C:\xampp\htdocs\api_test\example.php</b> on line <b>45</b><br />

并且在参考该函数的搜索中没有任何内容。我可以让curl请求很好但是这个多了什么东西杀了我。有什么帮助吗?

1 个答案:

答案 0 :(得分:1)

这是您正在遵循的教程中的自制函数。每条评论都解释了它在您所使用的教程中的作用.. ^ _ ^

// 15.将URL添加到多句柄

function add_url_to_multi_handle($mh, $url_list) {
    static $index = 0;

    // if we have another url to get
    if ($url_list[$index]) {

        // new curl handle
        $ch = curl_init();

        // set the url
        curl_setopt($ch, CURLOPT_URL, $url_list[$index]);
        // to prevent the response from being outputted
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        // follow redirections
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        // do not need the body. this saves bandwidth and time
        curl_setopt($ch, CURLOPT_NOBODY, 1);

        // add it to the multi handle
        curl_multi_add_handle($mh, $ch);


        // increment so next url is used next time
        $index++;

        return true;
    } else {

        // we are done adding new URLs
        return false;
    }
}