我想使用PHP和cURL
将以下代码转换为同步http请求 $select_ids = $db->query("SELECT job_id, tids FROM sent_items");
if ($select_ids->num_rows) {
while($row = $select_ids->fetch_object()) {
$records[] = $row;
}
$select_ids->free();
}
print_r($records);
if(!count($records)) {
echo 'No records';
} else {
foreach ($records as $r) {
$chandle = curl_init();
$url = "http://api.mvaayoo.com/apidlvr/APIDlvReport?user=USER ID:PASSWORD&tid=$r->tids&jobid=$r->job_id";
curl_setopt($chandle, CURLOPT_URL, $url);
curl_setopt($chandle, CURLOPT_RETURNTRANSFER, 1);
$curl_result[] = curl_exec($chandle);
}
}
的print_r($记录);返回jobid和tid
请参阅Here
我将工作ID和tid提供给api url
$url = "http://api.mvaayoo.com/apidlvr/APIDlvReport?user=USER ID:PASSWORD&tid=$r->tids&jobid=$r->job_id";
TID = $ R-> TIDS&安培;作业ID = $ R-> JOB_ID
$ curl_result []返回
请参阅Here
我的问题是如何将上面的代码转换为同时的http请求????
代码文章
答案 0 :(得分:1)
创建要与CURL一起使用的URL数组,并将它们发送到multiRequest
函数。请参阅文章中的GET示例。
因此,它最终将为每个URL创建CURL并发出请求。请求后,它会将结果返回$multi_curl_result
array
。然后你可以使用返回的数据。
<?php
// Add multiRequest function here from article
$select_ids = $db->query("SELECT job_id, tids FROM sent_items");
if ($select_ids->num_rows) {
while($row = $select_ids->fetch_object()) {
$records[] = $row;
}
$select_ids->free();
}
if(!count($records)) {
echo 'No records';
} else {
$curls = array();
foreach ($records as $r) {
$curls[] = "http://api.mvaayoo.com/apidlvr/APIDlvReport?user=USER ID:PASSWORD&tid=$r->tids&jobid=$r->job_id";
}
$multi_curl_result = multiRequest($curls);
}
您可以为CURL作业添加ID。然后,您可以通过ID从结果数组中获取数据。
$curls[$r->job_id] = "http://api.mvaayoo.com/apidlvr/APIDlvReport?user=USER ID:PASSWORD&tid=$r->tids&jobid=$r->job_id";
现在您知道哪些数据属于哪个作业。例如,如果job_id
为10
,则此作业的数据位于$multi_curl_result[10]
。
关于multiRequest功能如何工作的一些评论
multiRequest
函数(source)。
function multiRequest($data, $options = array()) {
// array of curl handles
$curly = array();
// data to be returned
$result = array();
// multi handle
// Allows the processing of multiple cURL handles asynchronously.
$mh = curl_multi_init();
// loop through $data and create curl handles
// then add them to the multi-handle
foreach ($data as $id => $d) {
// Create new CURL handle and add it to array
// $id makes it possible to detect which result belongs to which request
$curly[$id] = curl_init();
// If $data item is array, get URL from array, otherwise item should be URL (for GET requests)
$url = (is_array($d) && !empty($d['url'])) ? $d['url'] : $d;
// Set regular CURL options
curl_setopt($curly[$id], CURLOPT_URL, $url);
curl_setopt($curly[$id], CURLOPT_HEADER, 0);
curl_setopt($curly[$id], CURLOPT_RETURNTRANSFER, 1);
// If $data item is array, then we could have POST data
if (is_array($d)) {
// If item has POST data
if (!empty($d['post'])) {
// Set POST data
curl_setopt($curly[$id], CURLOPT_POST, 1);
curl_setopt($curly[$id], CURLOPT_POSTFIELDS, $d['post']);
}
}
// Set extra CURL options from array
if (!empty($options)) {
curl_setopt_array($curly[$id], $options);
}
// Add normal CURL handle to a CURL multi handle
curl_multi_add_handle($mh, $curly[$id]);
}
// execute the handles
// $running will have value, if CURL is still running
// Every execute will change it, until all requests has been done
$running = null;
do {
curl_multi_exec($mh, $running);
} while($running > 0);
// Add request responses to array
// Recognizable by $id
foreach($curly as $id => $c) {
// Return the content
$result[$id] = curl_multi_getcontent($c);
// Removes CURL handle ($c) from multi handle ($mh)
curl_multi_remove_handle($mh, $c);
}
// All done
curl_multi_close($mh);
return $result;
}
答案 1 :(得分:0)
简单地改变你的foreach就像:
$data = array();
foreach ($records as $r) {
$data[] = "http://api.mvaayoo.com/apidlvr/APIDlvReport?user=USER ID:PASSWORD&tid=$r->tids&jobid=$r->job_id";
}
$curl_result = multiRequest($data);
答案 2 :(得分:0)
你可以使用PHP的curl_multi_init,但是许多可用库中的一个可能比自己编写它更容易。
检查这些:
PHP-multi-curl:https://github.com/jmathai/php-multi-curl
和