Created
November 22, 2010 07:13
-
-
Save joshsmith/709628 to your computer and use it in GitHub Desktop.
DirectedEdge Bindings curl_multi for parallel cURL requests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class DirectedEdgeRest | |
{ | |
/** | |
* Gets multiple simultaneous recommendations from Directed Edge | |
* @param array $queryArray Array of the form array(0 => (array('item' => (string) $item, 'tags' => (string) $tags, 'limit' => (int) $limit)) | |
* | |
* @return array Multi-dimensional array containing responses to | |
* queries in the order they were passed in the array | |
*/ | |
public function getMultiRecommended($queryArray) | |
{ | |
$targetUrls = array(); | |
foreach($queryArray as $query) { | |
$targeturl = self::buildURL($query['item'], 'recommended', $query['tags'], $query['limit'], 'true'); | |
$targetUrls[] = $targeturl; | |
} | |
$responses = self::getMultiCurlResponses($targetUrls); | |
$xmlArray = array(); | |
foreach($responses as $response) { | |
$xmlArray[] = self::parseXML($response); | |
} | |
$count = count($xmlArray); | |
// Iterate through the XML and place IDs into an array | |
for($i = 0; $i < $count; $i++) { | |
foreach($xmlArray[$i]->item->recommended as $recommended) { | |
$recommendedResults[$i][] = filter_var($recommended, FILTER_SANITIZE_NUMBER_INT); | |
} | |
} | |
return $recommendedResults; | |
} | |
/** | |
* Returns the cURL responses given multiple target URLs | |
* @param array $targetUrls Array of target URLs for cURL | |
* | |
* @return array cURL Responses | |
*/ | |
private function getMultiCurlResponses($targetUrls) | |
{ | |
// Cache the count | |
$count = count($targetUrls); | |
// Create the multiple cURL handles | |
for($i = 0; $i < $count; $i++) { | |
$ch[$i] = curl_init($targetUrls[$i]); | |
curl_setopt($ch[$i], CURLOPT_POST, FALSE); | |
curl_setopt($ch[$i], CURLOPT_SSL_VERIFYPEER, FALSE); | |
curl_setopt($ch[$i], CURLOPT_RETURNTRANSFER, TRUE); | |
} | |
// Initialize the multiple cURL handle | |
$mh = curl_multi_init(); | |
// Add the handles to the curl_multi handle | |
for($i = 0; $i < $count; $i++) { | |
curl_multi_add_handle($mh, $ch[$i]); | |
} | |
$running=null; | |
// Execute the handles | |
do { | |
curl_multi_exec($mh,$running); | |
} while ($running > 0); | |
$responses = array(); | |
// Remove the handles and return the response | |
for($i = 0; $i < $count; $i++) { | |
curl_multi_remove_handle($mh, $ch[$i]); | |
$responses[$i] = curl_multi_getcontent($ch[$i]); | |
} | |
// Close the multiple cURL handle | |
curl_multi_close($mh); | |
return $responses; | |
} | |
} | |
$uid = 3; | |
$de = new DirectedEdgeRest(); | |
$query['item'] = "user".$uid; | |
$query['limit'] = 10; | |
$query['tags'] = "goal"; | |
$queryArray[0] = $query; | |
$query['tags'] = "question"; | |
$queryArray[1] = $query; | |
$recommended = $de->getMultiRecommended($queryArray); | |
echo '<pre>'; | |
var_dump($recommended); | |
// Outputs... | |
array(2) { | |
[0]=> | |
array(10) { | |
[0]=> | |
string(3) "141" | |
[1]=> | |
string(2) "64" | |
[2]=> | |
string(2) "37" | |
[3]=> | |
string(2) "65" | |
[4]=> | |
string(2) "63" | |
[5]=> | |
string(1) "7" | |
[6]=> | |
string(2) "78" | |
[7]=> | |
string(1) "9" | |
[8]=> | |
string(2) "30" | |
[9]=> | |
string(2) "10" | |
} | |
[1]=> | |
array(10) { | |
[0]=> | |
string(2) "97" | |
[1]=> | |
string(3) "125" | |
[2]=> | |
string(3) "133" | |
[3]=> | |
string(3) "127" | |
[4]=> | |
string(3) "101" | |
[5]=> | |
string(3) "134" | |
[6]=> | |
string(2) "69" | |
[7]=> | |
string(2) "80" | |
[8]=> | |
string(2) "19" | |
[9]=> | |
string(3) "129" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment