This is the code I am using
function initiate_curl($row, $mh) {
$ch = curl_init();
$url = 'http://openapi.gbis.go.kr/ws/rest/busarrivalservice'; /*URL*/
$queryParams = '?' . urlencode('serviceKey') . "SERVICE API KEY"; /*Service Key*/
$queryParams .= '&' . urlencode('stationId') . '=' . urlencode($row['stId']); /**/
$queryParams .= '&' . urlencode('routeId') . '=' . urlencode($row['busRouteId']); /*노선ID*/
$queryParams .= '&' . urlencode('staOrder') . '=' . urlencode($row['seq']);
curl_setopt($ch, CURLOPT_URL, $url . $queryParams);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_multi_add_handle($mh, $ch);
return $ch;
}
$mh = curl_multi_init();
$arr = array();
$rows = array();
while ($row = mysqli_fetch_array($query)) {
array_push($arr, initiate_curl($row, $mh));
array_push($rows, $row);
}
$running = null;
do {
curl_multi_exec($mh, $running);
} while ($running);
foreach($arr as $curl) {curl_multi_remove_handle($mh, $curl);}
curl_multi_close($mh);
foreach($arr as $key=>$curl) {
**DO MY WORK
}
Most of the times it works fine but sometimes I get null as a result of the query. However, when I go to the API through typing the url, it returns the value perfectly. Also, the object that is returned as null changes any time so I'm 100% sure that this is the fault of my curl_multi part.
Am I implementing this wrong or is it simply not that reliable to use?