
Acelerar curl ayuda!
Publicado por Junior (11 intervenciones) el 10/02/2017 04:34:13
Buenas noches!, soy nuevo en esta comunidad y tengo un problema con mi script, se tarda mucho en visualizar los datos solicitados por la librería curl, estoy usando la api v3 de youtube, aquí mi código aver si alguien me ayuda optimizando el código.
-
-
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?
$clave = 'api youtube v3';
$api = 'https://www.googleapis.com/youtube/v3/';
$c_result = '15';
if(empty($_POST['q']))
{
$q = 'olimpopt';
}else{
$q = str_replace(" ","%20",$_POST['q']);
}
function videos($id)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, 'https://www.googleapis.com/youtube/v3/videos?part=contentDetails,statistics&id='.$id.'&key=AIzaSyAs-m1Tnn6dTZW5LdZedexte1WPYTANiYE');
$result = curl_exec($ch);
curl_close($ch);
$json = json_decode($result, true);
$tiempo = $json['items'][0]['contentDetails']['duration'];
$view = $json['items'][0]['statistics']['viewCount'];
return array($tiempo,$view);
}
//inicio curl
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $api.'search?q='.$q.'&part=snippet&key='.$clave.'&maxResults='.$c_result);
$result = curl_exec($ch);
curl_close($ch);
$obj = json_decode($result);
$titulo = array();
$desc = array();
$video_id = array();
foreach($obj as $valor) {
$e=$e+1;
$total[$e] = $valor->totalResults;
}
foreach($obj->items as $value) {
$c=$c+1;
$titulo[$c] = $value->snippet->title;
$video_id[$c] = $value->id->videoId;
$desc[$c] = $value->snippet->description;
}
$t_result = $total[5];
echo '<form action="index.php" method="post">';
echo '<center>';
echo '<label>Buscar : </label><input type="text" name="q" /> ';
echo ' <input type="submit" name="buscar" value="Buscar" />';
echo '</center>';
echo '</form>';
$num = count($video_id);
$videos = array();
$tiempo = array();
for ($j=1;$j<=$num;$j++)
{
$videos[$j] = videos($video_id[$j]);
}
for($k=1;$k<=$num;$k++)
{
$tiempo[$k] = $videos[$k][0];
$tiempo[$k] = str_replace("PT","",$tiempo[$k]);
$tiempo[$k] = str_replace("H",":",$tiempo[$k]);
$tiempo[$k] = str_replace("M",":",$tiempo[$k]);
$tiempo[$k] = str_replace("S","",$tiempo[$k]);
$view[$k] = $videos[$k][1];
}
echo '<table align="center">';
echo '<th colspan="2">'.count($video_id).' resultados de '.$t_result.'</th>';
for($i=1;$i<=count($video_id);$i++)
{
?>
<tr>
<td><a href="javascript:videito('<? echo $video_id[$i]; ?>','<? echo $titulo[$i]; ?>')"><img src="http://i4.ytimg.com/vi/<? echo $video_id[$i]; ?>/default.jpg" class="DereChuWapaiMg" width="75" height="65" border=0></a></td>
<td><font face="Verdana" size="-2"><div class="video-mini-title">
<a href="vervideo.php?link=<? echo $video_id[$i]; ?>&titulo=<? echo $titulo[$i]; ?>" target="_blank"><b><font class="navtext" face="Arial" size="2" style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px; color: #D9D9D9; font-weight: bold"><? echo $titulo[$i]; ?></font></b></a></div>
<div class="video-view-count"><b>Visitas:</b> <? echo $view[$i]; ?></div>
<div class="video-username"><b>Tiempo:</b> <? echo $tiempo[$i]; ?></div></font>
</td>
</tr>
<?
}
?>
</table>
Valora esta pregunta


0