我想使用 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($records); returns jobid and 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&jobid=$r->job_id
$curl_result[] returns
参见 Here
我的问题是如何将上面的代码转换为同步的 http 请求????
代码文章
最佳答案
创建要与 CURL 一起使用的 URL 数组,并将它们发送到 multiRequest
函数。请参阅文章中的 GET 示例。
因此它最终会为这些 URL 中的每一个创建 CURL 并发出请求。请求后,它会将结果作为 array
返回到 $multi_curl_result
中。然后你可以使用返回的数据。
<?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);
}
您可以将 ID 添加到 CURL 作业。然后你可以通过 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;
}
关于php - 如何使用 cURL 在 PHP 中将我的代码转换为同步 http 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31934471/