PHP - Obtener datos con cURL y PHP

 
Vista:
Imágen de perfil de Jorge Luis
Val: 27
Ha aumentado su posición en 69 puestos en PHP (en relación al último mes)
Gráfica de PHP

Obtener datos con cURL y PHP

Publicado por Jorge Luis (7 intervenciones) el 07/07/2017 01:47:01
Hola!

necesito ayuda, estoy intentando obtener un dato de una pagina de la cual estoy consultando con php y cURL, pero no esta dejando, espero me puedan ayudar dejo el codigo.

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
88
89
<?php
date_default_timezone_set('America/Mexico_City');
$rpu = $_POST['rpu'];
$rpu = str_replace('-', '', $rpu);
//$URL = "https://switch.mx/beta/user.do?action=login"; 
//$conex = curl_init(); 
$ch = curl_init("https://switch.mx/beta/user.do?action=login");
 
$data = array("holder" => "amigo", "rpu" => $rpu ) ;
$total= array("configData"=>$data,"providerId" => "1");
 
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_COOKIE, "cookie.txt");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
 
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(array("login"=>"user@user.com","password"=>"UsEr123")));
curl_exec ($ch);
 
curl_setopt($ch, CURLOPT_URL, 'https://switch.mx/beta/services.do?action=save');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($total));
$era=  curl_exec ($ch);
$ena= json_decode($era,true);
 
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_POST, false);
curl_setopt($ch, CURLOPT_HTTPGET, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
//107615135684 
curl_setopt($ch, CURLOPT_URL, 'https://switch.mx/beta/services.do?action=details&serviceId='.$ena['id']);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
$consumos= curl_exec ($ch);
 
curl_setopt($ch, CURLOPT_URL, "https://switch.mx/beta/services.do?action=find");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
$servicio= curl_exec ($ch);
 
$decodificado=json_decode($servicio, true);
 
 
$consume=json_decode($consumos, true);
$yacon=$consume['serviceData'];
 
$res=array();
$i=0;
$dias=0;
foreach ($yacon as $name => $value){
     $ini = strtotime($value['startDate']);
     $fin = strtotime($value['endDate']);
     $datediff = $fin - $ini;
     $dias += floor($datediff/(60*60*24));
    setlocale(LC_ALL, 'es_ES').': ';
    $res[$i]=array("inicio"=>strtoupper(date('M y', strtotime($value['startDate']))),"final"=>strtoupper(date('M y', strtotime($value['endDate']))),"consumo"=>$value['consumption']);
    $i++;
    if ($dias>=340) {
        break;
    }
}
 
curl_setopt($ch, CURLOPT_URL, 'https://switch.mx/beta/features.do?ServiceId='.$ena['id'].'&rpu='.$rpu.'&name='.$rpu.'&featureId=6&providerId=1');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('application/x-www-form-urlencoded'));
$data = curl_exec ($ch);
$error = curl_error($ch);
 
 
$destination = "recibos/".$rpu.".pdf";
$file = fopen($destination, "a+");
fputs($file, $data);
fclose($file);
 
 
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
 
curl_setopt($ch, CURLOPT_URL, 'https://switch.mx/beta/services.do?action=delete');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($decodificado['0']));
echo curl_exec ($ch);
curl_close ($ch);
 
 
echo json_encode($res);
?>
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder