2013-04-17 22 views
0

這可能是一個愚蠢的問題,但我只是想知道如果這是可能的,或者如果我應該做別的事情...PHP:使用某些庫中的方法而不是URL進行多捲曲?

當使用多捲曲的人會使用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.. 

每多捲曲document ...

那麼,如果我有一些方法(我認爲這就是你怎麼稱呼它)我是從一個library

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

現在這就是搜索詞tag的圖書館。但是,如果我希望能夠做多次搜索什麼,

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

如何落實到多捲曲呢?它只是簡單地用$tags1tags2替換網址嗎?

+0

你能告訴更多關於爲什麼你需要cURL多處理程序嗎?我想你可以在需要連接時創建一個新的處理程序。 –

+0

這與多重cURL相關的方式是什麼? – silkfire

+0

@ Trung-HieuLe @silkfire,因爲我需要一次執行請求,而不是一次完成一個請求。現在,如果我請求'$ tags1',我必須等到完成後纔會請求'$ tags2' – hellomello

回答

0

這是沒有你的課,不明白你爲什麼需要這個。

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); 
0

我已經看過成PHP庫無水發現Instagram類只使用一個捲曲處理,這導致了事實上的每個實例,你不能異步發送多個請求。

您可以閱讀這篇文章about connection Sharing with CURL in PHP以獲得修改Instagram庫的CurlClient類的想法。這裏的主要思想是保留一個靜態類成員,它持有來自curl_multi_init()的處理程序,並在需要時爲其添加每個新的cURL單處理程序。