如何重构这个 cURL 脚本以利用 PHP 的 curl_multi 函数?
我在 PHP 应用程序中使用 cURL 连接到 RESTful API。然而,我最近发现我没有并行化我的 cURL 连接,因此执行多个连续连接会导致最终用户出现极大的延迟。
我以前没有使用过curl_multi
,读完文档后我有点不知所措。如何最好地重构以下代码以利用 curl_multi
的并行化?
编辑:我忘了提及我开源了此处使用的 API。这些是我自己的 Directed Edge PHP 绑定。因此,如果您愿意,您还可以将此处的帮助合并到 GitHub 上的代码中,并且您将被列为贡献者。
以下是我在客户端代码中所做的示例:
// Get 100 goal recommendations from Directed Edge
$de = new DirectedEdgeRest();
$item = "user".$uid;
$limit = 100;
$tags = "goal";
$recommendedGoals = $de->getRecommended($item, $tags, $limit);
// Get 100 interest recommendations from Directed Edge
$de = new DirectedEdgeRest();
$item = "user".$uid;
$limit = 100;
$tags = "interest";
$recommendedInterests = $de->getRecommended($item, $tags, $limit);
以下是 DirectedEdgeRest()
中的相关函数
/**
* Returns array of recommended result IDs for an item
* @param string $item Item, e.g. "Miles%20Davis"
* @param string $tags Tags as comma delimited string, e.g. "product,page"
* @param int $limit Limit for max results
*
* @return array Recommended result IDs
*/
public function getRecommended($item, $tags, $limit)
{
// Connect to Directed Edge and parse the returned XML
$targeturl = self::buildURL($item, 'recommended', $tags, $limit, 'true');
$response = self::getCurlResponse($targeturl);
$xml = self::parseXML($response);
// Iterate through the XML and place IDs into an array
foreach($xml->item->recommended as $recommended) {
$recommendedResults[] = filter_var($recommended, FILTER_SANITIZE_NUMBER_INT);
}
return $recommendedResults;
}
/**
* Builds URL for cURL
* @param string $item Item, e.g. "Miles%20Davis"
* @param string $type Type of API request: either "related" or "recommended"
* @param string $tags Tags as comma delimited string, e.g. "product,page"
* @param int $limit Limit for max results
* @param string $exclude "true" if you want to exclude linked, "false" otherwise
*
* @return string The target URL
*/
private function buildURL($item, $type, $tags, $limit, $exclude)
{
$targeturl = DE_BASE_URL;
$targeturl .= $item; // Item
$targeturl .= "/" . $type; // Type
$targeturl .= "?tags=" . $tags; // Tags
$targeturl .= "&maxresults=" . $limit; // Limit
$targeturl .= "&excludeLinked=" . $exclude; // Exclude
return $targeturl;
}
/**
* Returns the cURL response given a target URL
* @param string $targeturl The target URL for cURL
*
* @return string cURL Response
*/
private function getCurlResponse($targeturl)
{
$ch = curl_init($targeturl);
curl_setopt($ch, CURLOPT_POST, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
I'm using cURL in my PHP app to connect to a RESTful API. However, I just recently discovered that I'm not parallelizing my cURL connections and so performing several successive connection results in extreme latency for the end-user.
I've not used curl_multi
before and I'm kind of at a loss after reading through the documentation. How can I best refactor the following code to take advantage of curl_multi
's parallelization?
EDIT: I forgot to mention that I open-sourced the API that's being used here. These are my own Directed Edge PHP bindings. So if you'd like, you can also have your help here merged into the code on GitHub and you'll be listed as a contributor.
Here's an example of what I'm doing in the client code:
// Get 100 goal recommendations from Directed Edge
$de = new DirectedEdgeRest();
$item = "user".$uid;
$limit = 100;
$tags = "goal";
$recommendedGoals = $de->getRecommended($item, $tags, $limit);
// Get 100 interest recommendations from Directed Edge
$de = new DirectedEdgeRest();
$item = "user".$uid;
$limit = 100;
$tags = "interest";
$recommendedInterests = $de->getRecommended($item, $tags, $limit);
And here are the relevant functions from DirectedEdgeRest()
/**
* Returns array of recommended result IDs for an item
* @param string $item Item, e.g. "Miles%20Davis"
* @param string $tags Tags as comma delimited string, e.g. "product,page"
* @param int $limit Limit for max results
*
* @return array Recommended result IDs
*/
public function getRecommended($item, $tags, $limit)
{
// Connect to Directed Edge and parse the returned XML
$targeturl = self::buildURL($item, 'recommended', $tags, $limit, 'true');
$response = self::getCurlResponse($targeturl);
$xml = self::parseXML($response);
// Iterate through the XML and place IDs into an array
foreach($xml->item->recommended as $recommended) {
$recommendedResults[] = filter_var($recommended, FILTER_SANITIZE_NUMBER_INT);
}
return $recommendedResults;
}
/**
* Builds URL for cURL
* @param string $item Item, e.g. "Miles%20Davis"
* @param string $type Type of API request: either "related" or "recommended"
* @param string $tags Tags as comma delimited string, e.g. "product,page"
* @param int $limit Limit for max results
* @param string $exclude "true" if you want to exclude linked, "false" otherwise
*
* @return string The target URL
*/
private function buildURL($item, $type, $tags, $limit, $exclude)
{
$targeturl = DE_BASE_URL;
$targeturl .= $item; // Item
$targeturl .= "/" . $type; // Type
$targeturl .= "?tags=" . $tags; // Tags
$targeturl .= "&maxresults=" . $limit; // Limit
$targeturl .= "&excludeLinked=" . $exclude; // Exclude
return $targeturl;
}
/**
* Returns the cURL response given a target URL
* @param string $targeturl The target URL for cURL
*
* @return string cURL Response
*/
private function getCurlResponse($targeturl)
{
$ch = curl_init($targeturl);
curl_setopt($ch, CURLOPT_POST, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在你提出问题之前我并不知道
curl_multi
,这是一个非常奇怪的(对于PHP)接口。看起来有一个 curl_multi_init 文档中的 Hello World 示例
我就从那开始。
I wasn't aware of
curl_multi
before your question, that's a pretty bizarre (for PHP) interface.It looks like there's a Hello World example in the curl_multi_init documentation
I'd start with that.
如果有人感兴趣,以下是我如何重构代码以使用
curl_multi
。如果这一切看起来很酷,或者如果你可以,请为绑定做出贡献做得更好!In case anyone's interested, here's how I refactored the code to make use of
curl_multi
. And please, contribute to the bindings if this all seems cool, or if you can do a better job!