Foros del Web » Programando para Internet » PHP »

[SOLUCIONADO] Curl con php

Estas en el tema de Curl con php en el foro de PHP en Foros del Web. Hola gente! Estoy utilizando Curl para obtener el valor de "adcopy_challenge" que te devuelve la url "http://api.solvemedia.com/papi/challenge.script?k=MIqUIMADf7KbDRf0ANI-9wLP.8iJSG9N" pero no lo consigo. Tengo los siguientes archivos: ...
  #1 (permalink)  
Antiguo 02/12/2013, 01:24
 
Fecha de Ingreso: junio-2011
Mensajes: 36
Antigüedad: 12 años, 10 meses
Puntos: 1
Curl con php

Hola gente!

Estoy utilizando Curl para obtener el valor de "adcopy_challenge" que te devuelve la url "http://api.solvemedia.com/papi/challenge.script?k=MIqUIMADf7KbDRf0ANI-9wLP.8iJSG9N" pero no lo consigo.

Tengo los siguientes archivos:

api.php
---------------------------------------------------------------------------------

<script type="text/javascript" src="http://api.solvemedia.com/papi/challenge.script?k=MIqUIMADf7KbDRf0ANI-9wLP.8iJSG9N">
</script>

---------------------------------------------------------------------------------



carga.php
---------------------------------------------------------------------------------

<?php

$url ="http://localhost/api.php";

$curl = curl_init();
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_AUTOREFERER, 0);
curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_URL, $url);
$data = curl_exec ($curl);

echo $data;

print_r (explode("adcopy_challenge", $data));


?>

---------------------------------------------------------------------------------


Cuando ejecuto el archivo carga.php obtengo como resultado del explode:

---------------------------------------------------------------------------------

Array ( [0] => )

---------------------------------------------------------------------------------


Como que no lo encuentra.
Y si me fijo en "inspeccionar elemento" de Google Chrome me aparece:

---------------------------------------------------------------------------------

<html><head><script async="" src="http://b.scorecardresearch.com/beacon.js"></script><script type="text/javascript" src="http://api.solvemedia.com/papi/challenge.script?k=MIqUIMADf7KbDRf0ANI-9wLP.8iJSG9N">
</script><script type="text/javascript" src="//api.solvemedia.com/papi/_puzzle.js"></script><style type="text/css"> #adcopy-outer{background-color:#f8f8f8;color:#000;border:1px solid #bbb;border-radius:4px;-moz-border-radius:4px;-webkit-border-radius:4px;width:300px;padding:3px;margin:0px;} #adcopy-outer tbody,#adcopy-outer table,#adcopy-outer span,#adcopy-outer tr,#adcopy-outer td,#adcopy-outer a,#adcopy-outer img{padding:0 !important;margin:0 !important;border:0 !important;line-height:normal !important;} #adcopy-outer td{white-space:nowrap !important;} #adcopy-instr,#adcopy-error-msg{font-size:10pt;font-family:sans-serif;} #adcopy-instr{color:#000;display:inline;} #adcopy-error-msg{color:#f00;display:none;} #adcopy-outer #adcopy-logo A:hover{background-color:transparent;} #adcopy-outer #adcopy-logo{color:#000;display:inline;} #adcopy-outer A{color:#004;text-decoration:none;} #adcopy-link-image,#adcopy-pixel-image,#adcopy_challenge_container{display:none;} #adcopy-outer #adcopy-link-logo img{vertical-align:middle;} #adcopy-link-refresh,#adcopy-link-info{display:inline;} #adcopy-outer #adcopy-link-refresh img,#adcopy-outer #adcopy-link-info img,#adcopy-outer #adcopy-link-audio img,#adcopy-outer #adcopy-link-image img{vertical-align:middle;} #adcopy-puzzle-image{height:150px;width:300px;} #adcopy-puzzle-image img{float:none !important;} #adcopy-outer #adcopy_response{width:185px !important;margin:2px 0 0 0;font-size:12px;} #adcopy-response-cell{height:23px;} @-webkit-keyframes adcopy-bouncer{0%,6%,12%,18%,24%,100%{-webkit-transform:translate(0,0);} 3%{-webkit-transform:translate(1px,-10px);} 9%{-webkit-transform:translate(0px,8px);} 15%{-webkit-transform:translate(0px,-5px);} 21%{-webkit-transform:translate(1px,2px);} } @-moz-keyframes adcopy-bouncer{0%,6%,12%,18%,24%,100%{-moz-transform:translate(0,0);} 3%{-moz-transform:translate(1px,-10px);} 9%{-moz-transform:translate(0px,8px);} 15%{-moz-transform:translate(0px,-5px);} 21%{-moz-transform:translate(1px,2px);} } .adcopy-info-bouncy img{-webkit-animation:adcopy-bouncer 2s infinite;-moz-animation:adcopy-bouncer 2s infinite;} .adcopy-info-bouncy img:hover{-webkit-animation:none;-moz-animation:none;} </style> <script type="text/javascript" src="//api.solvemedia.com/papi/_challenge.js?k=MIqUIMADf7KbDRf0ANI-9wLP.8iJSG9N;f=_ACPuzzleUtil.callbacks%5B0%5D;l=en ;t=img;s=standard;c=js,h5c,h5ct,svg,h5v,v/h264,v/ogg,v/webm,h5a,a/mp3,a/ogg,ua/chrome,ua/chrome31,os/nt,os/nt6.2,swf11,swf11.9,swf,fwv/NulK0g.jxxk35;am=w07QU-KaHZ71p5tr4podng;ca=script;ts=1385967287;ct=138596 7453;th=white;r=0.8756890976801515"></script></head><body><div id="adcopy-outer"> <table cellpadding="0" cellspacing="0"> <tbody><tr><td colspan="3"><div id="adcopy-puzzle-image"><img src="//api.solvemedia.com/papi/[email protected]@Vql8wtAoiR4ENU8bgrS3.X9APTC4lJjoX69.h NzcGY5jhZemRXCWVqudppsnwo4q-KTQr6IcP4-HqtHhQljShQ.g5JbxVPC.LWo5uU3ZxQUHPZmuSoQDS9Lqm.WTe ySrW6jpznfjzb2fcItjViazdxl4ixain6M-HFOM2x3mPsjAHxyIrEHfVBqgeBrxlODgoddct.NLM9E0P79OwV Amk-vbXNztAtka9jwNWHtLANyiMoHE5oOJ8RLCmrV0igplJcVSNb2d lVTu2vzTGWOblZN49fQe60kGJkhFXIK0uoA;w=300;h=150;fg =000000;bg=f8f8f8" alt="Solve Media Puzzle Challenge" height="150" width="300" id="adcopy-puzzle-image-image"></div> </td></tr> <tr id="adcopy-instr-row"><td><span id="adcopy-instr" style="display: inline;">Your Answer</span><span id="adcopy-error-msg" style="display: none;"></span></td> <td><div id="adcopy-pixel-image" style="display:none;"><br><img src="http://map.media6degrees.com/orbserv/hbpix?pixId=16483&amp;pcv=60" alt="" height="1" width="1"><img src="http://adadvisor.net/adscores/r.pixel?sid=9212136498" alt="" height="1" width="1">
<script>
window._comscore = window._comscore || [];

_comscore.push({ c1: "8", c2: "14651931", c3: "1000000000000000001" });

(function() {
var s = document.createElement("script"), el = document.getElementsByTagName("script")[0]; s.async = true;
s.src = (document.location.protocol == "https:" ? "https://sb" : "http://b") + ".scorecardresearch.com/beacon.js";
el.parentNode.insertBefore(s, el);
})();
</script>

<iframe width="0" height="0" frameborder="0" src="http://loadus.exelator.com/load/?p=490&amp;g=003&amp;c=&amp;kw=&amp;refkw=&amp;j=w "></iframe>
</div></td> <td align="center"><span id="adcopy-logo"><a href="javascript:ACPuzzle.moreinfo('')" id="adcopy-link-logo" title=""><img src="//api.solvemedia.com/media/solve-whV2.png" alt="Solve Media"></a></span></td> </tr> <tr> <td id="adcopy-response-cell"><input type="text" name="adcopy_response" autocomplete="off" autocorrect="off" size="20" id="adcopy_response"><span id="adcopy_challenge_container"><input type="hidden" name="adcopy_challenge" id="adcopy_challenge" value="[email protected]@Vql8wtAoiR4ENU8bgrS3.X9APTC4lJjoX69.h NzcGY5jhZemRXCWVqudppsnwo4q-KTQr6IcP4-HqtHhQljShQ.g5JbxVPC.LWo5uU3ZxQUHPZmuSoQDS9Lqm.WTe ySrW6jpznfjzb2fcItjViazdxl4ixain6M-HFOM2x3mPsjAHxyIrEHfVBqgeBrxlODgoddct.NLM9E0P79OwV Amk-vbXNztAtka9jwNWHtLANyiMoHE5oOJ8RLCmrV0igplJcVSNb2d lVTu2vzTGWOblZN49fQe60kGJkhFXIK0uoA"></span></td> <td></td> <td align="center"><a href="javascript:ACPuzzle.reload('')" id="adcopy-link-refresh" title="New Puzzle"><img src="//api.solvemedia.com/media/reload-whV2.gif" alt="New Puzzle"></a><a href="javascript:ACPuzzle.change2audio('')" id="adcopy-link-audio" title="Switch to audio puzzle" style="display: inline;"><img src="//api.solvemedia.com/media/note-whV2.gif" alt="Switch to audio puzzle"></a><a href="javascript:ACPuzzle.change2image('')" id="adcopy-link-image" title="Switch to visual puzzle" style="display: none;"><img src="//api.solvemedia.com/media/text-whV2.gif" alt="Switch to visual puzzle"></a><a href="javascript:ACPuzzle.moreinfo('')" id="adcopy-link-info" title="More Information..." class=""><img src="//api.solvemedia.com/media/info-whV2.gif" alt="More Information..."></a></td> </tr> </tbody></table> </div>

</body></html>

---------------------------------------------------------------------------------


Lo que está puesto en negrita y rojo es la ubicación de adcopy_challenge y su respectivo valor que es lo que quiero obtener.

Se les ocurre alguna idea para obtener ese dato?
  #2 (permalink)  
Antiguo 02/12/2013, 05:20
 
Fecha de Ingreso: noviembre-2013
Mensajes: 10
Antigüedad: 10 años, 5 meses
Puntos: 0
Respuesta: Curl con php

Prueba con esto:

$patron="id=\"adcopy_challenge\" value=\"(.*?)\"></span>";
preg_match_all("($patron)iU", $data, $resultado);

echo $resultado[1][0];
  #3 (permalink)  
Antiguo 02/12/2013, 14:28
 
Fecha de Ingreso: junio-2011
Mensajes: 36
Antigüedad: 12 años, 10 meses
Puntos: 1
Respuesta: Curl con php

Gracias por responder! He puesto el código que me dijiste pedazovago pero me genera el siguiente error:

---------------------------------------------------------------------------------------------

( ! ) Notice: Undefined offset: 0 in C:\wamp\www\carga.php on line 25
Call Stack
# Time Memory Function Location
1 0.0004 144136 {main}( ) ..\carga.php:0

---------------------------------------------------------------------------------------------

Además si modifico el archivo carga.php por el siguiente:


---------------------------------------------------------------------------------------------

<?php

$url ="http://localhost/api.php";

$curl = curl_init();
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_AUTOREFERER, 0);
curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_URL, $url);
$data = curl_exec ($curl);

echo htmlentities($data);

?>

---------------------------------------------------------------------------------------------


Me tira como respuesta:

---------------------------------------------------------------------------------------------

<script type="text/javascript" src="http://api.solvemedia.com/papi/challenge.script?k=MIqUIMADf7KbDRf0ANI-9wLP.8iJSG9N"> </script>

---------------------------------------------------------------------------------------------


Por lo que me da a pensar que el hacer curl no me extrae el código por completo sino una parte, motivo por la cual me da el error mencionado anteriormente si pongo el código que me dijiste pedazovago porque nunca encuentra esa parte. Dentro de http://api.solvemedia.com/papi/challenge.script?k=MIqUIMADf7KbDRf0ANI-9wLP.8iJSG9N llama a otro script que es la encargada de crear el "adcopy_challenge" que es el quiero obtener. Pero estos datos no aparecen cuando hago curl sino en la propia página web al hacer "inspeccionar elemento" con Chrome.

Alguna idea más de cómo podría extraer el código completo generado con curl?

Muchas gracias a todos.
  #4 (permalink)  
Antiguo 02/12/2013, 15:11
Avatar de Dalam  
Fecha de Ingreso: septiembre-2010
Mensajes: 409
Antigüedad: 13 años, 7 meses
Puntos: 56
Respuesta: Curl con php

La pagina http://api.solvemedia.com/papi/chall...I-9wLP.8iJSG9N hace referencia a un juego de un puzzle o algo parecido.
Lo que quieres es el archivo?
  #5 (permalink)  
Antiguo 02/12/2013, 15:18
 
Fecha de Ingreso: junio-2011
Mensajes: 36
Antigüedad: 12 años, 10 meses
Puntos: 1
Respuesta: Curl con php

No, Dalam. Lo que quiero es el valor de "adcopy_challenge" que genera esa página.
Lo que estuve investigando es que con Curl no se puede obtener valores que te da un javascript porque da valores a nivel servidor, por lo que estoy intentando no se puede con curl php. Pero no se preocupen ya lo solucioné. Finalmente lo tuve que programar en Java. El código en Java es el siguiente para que alguien más le pueda ayudar:

---------------------------------------------------------------------------------------------------------------

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

package pruebas;

import java.io.*;
import com.gargoylesoftware.htmlunit.*;
import com.gargoylesoftware.htmlunit.html.*;

public class pruebas {

public static void main(String[] args) throws IOException {
String strUrl = "http://localhost/api.php";
final WebClient webClient = new WebClient();
HtmlPage pagina;
try {
pagina = webClient.getPage(strUrl);
HtmlElement elemento = pagina.getBody().getElementById("adcopy_challenge" );
System.out.println(elemento.getAttribute("value")) ;
} catch (FailingHttpStatusCodeException ex) {
System.out.println("Error obteniendo la página: " + ex.getMessage());
return;
}
}
}

---------------------------------------------------------------------------------------------------------------

Para más información de "HtmlUnit" pueden visitar la página:

http://blog.openalfa.com/como-trabajar-con-un-navegador-con-javascript-en-java/



Muchas gracias a todos por la ayuda. Les mando un abrazo fuerte!

Etiquetas: curl, javascript
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 09:13.