PHP:使用一些库中的方法而不是url进行多Curl操作


PHP: Multi Curl using Methods from some Library instead of URLs?

这可能是一个愚蠢的问题,但我只是想知道这是可能的,或者如果我应该做别的事情…

当使用多旋一个将使用url对吗?

// create both cURL resources
$ch1 = curl_init();
$ch2 = curl_init();
// set URL and other appropriate options
curl_setopt($ch1, CURLOPT_URL, "http://lxr.php.net/");
curl_setopt($ch1, CURLOPT_HEADER, 0);
curl_setopt($ch2, CURLOPT_URL, "http://www.php.net/");
curl_setopt($ch2, CURLOPT_HEADER, 0);
etc..

根据multi-curl文档…

如果我有一些方法(我想你们是这么称呼它的)我从库中使用

$tags = $instagram->searchTags( 'tag' );

现在正在搜索单词tag的库。但如果我想进行多次搜索呢,

$tags1 = $instagram->searchTags( 'tag' );
$tags2 = $instagram->searchTags( 'tagme' );

我如何实现这个多旋?它只是简单地用$tags1tags2替换url吗?

这是没有你的类,不明白为什么你需要它。

function fetchHTML($website) {
  if(function_exists('curl_init')) {
    $ch = curl_init($website);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
    $content = curl_exec($ch);
    curl_close($ch);
  } else {
    $content = file_get_contents($website)
  }
  return $content;
}
$dom = new DOMDocument();
$dom->loadHTML(fetchHTML("http://example1.com"));
$tag1 = $dom->getElementsByTagName('tagname');
$dom->loadHTML(fetchHTML("http://example2.com"));
$tag2 = $dom->getElementsByTagName('tagname');
/* Will give you a DOM object list with your first tagname */
print_r($tag1);
/* Will give you a DOM object list with your second tagname */
print_r($tag2);

我研究了PHP库,发现Instagram类的每个实例只使用一个cURL处理程序,这导致您无法异步发送多个请求。

您可以阅读这篇关于在PHP中使用CURL共享连接的文章,了解如何修改Instagram库的CurlClient类。这里的主要思想是保持一个静态类成员,其中包含curl_multi_init()的处理程序,并在需要时向其添加每个新的cURL单处理程序。