我正在开发一个从实际工作中输出数据的网站,该脚本正在运行,但我注意到数据与显示相同功能的其他网站相比显示速度非常慢,所以我想知道我是否可以优化我的代码到实现更好的结果。上面我留下代码:
<?php
function curl_request(){
// Get cURL resource
$curl = curl_init();
// Set some options
$country = "pt";
$jobType = "PHP;
$city = "Belem";
curl_setopt_array($curl, array(
// Return the response as a string instead of outputting it to the screen
CURLOPT_RETURNTRANSFER => 1,
//URL to send request to
CURLOPT_URL => 'http://api.indeed.com/ads/apisearch?publisher=xxxxxxxxxxxxx&q='.$jobType.'&l='.$city.'&sort=&radius=&st=&jt=&start=&limit=&fromage=&filter=&latlong=1&co='.$country.'&chnl=&userip=1.2.3.4&useragent=Mozilla/%2F4.0%28Firefox%29&v=2',
));
// Send the request & save response to $resp
$resp = curl_exec($curl);
// Close request to clear up some resources
curl_close($curl);
return $resp;
}
function xmlToArray($input, $callback = null, $recurse = false) {
$data = ((!$recurse) && is_string($input))? simplexml_load_string($input, 'SimpleXMLElement', LIBXML_NOCDATA): $input;
if ($data instanceof SimpleXMLElement) $data = (array) $data;
if (is_array($data)) foreach ($data as &$item) $item = xmlToArray($item, $callback, true);
return (!is_array($data) && is_callable($callback))? call_user_func($callback, $data): $data;
}
?>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<ol>
<?php
for($i=0;$i<25;$i++){ // using for loop to show number of jobs
$resp=curl_request($i);
$arrXml = xmlToArray($resp);
$results=$arrXml['results'];
?>
<li>
<p>
<strong>Job :<a href="<?php echo $results['result'][$i]['url']; ?>" target="_blank"><?php echo $results['result'][$i]['jobtitle']; ?></a></strong>
</p>
<p><strong>Location: <?php echo $results['result'][$i]['city']; ?></strong></p>
<p><strong>Date :<?php echo $results['result'][$i]['date'];?></strong></p>
<p> Descriptions :<?php echo $results['result'][$i]['snippet']; ?></p>
</li>
<?php } ?>
</ol>
</body>
</html>