La función os la dejo a continuación:
function getURLContent($url){ $doc = new DOMDocument; $doc->preserveWhiteSpace = FALSE; @$doc->loadHTMLFile($url); return $doc->saveHTML(); }
function getURLContent($url){ $doc = new DOMDocument; $doc->preserveWhiteSpace = FALSE; @$doc->loadHTMLFile($url); return $doc->saveHTML(); }
function getUrlContent($url){ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); curl_setopt($ch, CURLOPT_TIMEOUT, 5); $data = curl_exec($ch); $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); return ($httpcode>=200 && $httpcode<300) ? $data : false; }
function getUrls($url){ // obtenemos el contenido de la url $content = getUrlContent($url); if ($content==false){ return false; } // creamos el array de resultados $urls = array(); // creamos una instanacia a DOMDocument $doc = new DOMDocument; $doc->preserveWhiteSpace = false; // cargamos la pagina web desdecargada desde la función getUrlContent() @$doc->loadHTML($content); // buscamos todos los tags del tipo < a > $links = $doc->getElementsByTagName("a"); // recorremos cada uno de los tags encontrados foreach ($links as $link){ // obtenemos la parte href de la etiqueta $href = $link->getAttribute("href"); if (strlen($href)>=4 && substr($href,0,4)=="http"){ // url entera $urls[] = $href; } else if (strlen($href)>0 && substr($href, 0, 1)=="/"){ // url empieza por / le añadimos el dominio por delante $urls[] = $url.$href; }else{ // arhivo $urls[] = $url.'/'.$href; } } // eliminamos duplicados $urls = array_unique($urls); // ordenamos alfabéticamente sort($urls); return $urls; }Una vez hecho esto ya solamente nos quedará la llamada a la función y el mostrar en pantalla el resultado:
// especificamos la url $url = "http://softontherocks.blogspot.com.es"; // obtenemos las url $urls = getUrls($url); // mostramos los resultados if ($urls==false){ echo "La página no es correcta, o es una redirección."; } else { echo nl2br(implode($urls, "\n")); }
function getUrlContent($url){ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); curl_setopt($ch, CURLOPT_TIMEOUT, 5); $data = curl_exec($ch); $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); return ($httpcode>=200 && $httpcode<300) ? $data : false; }
function writeLog($data) { list($usec, $sec) = explode(' ', microtime()); $datetime = strftime("%Y%m%d %H:%M:%S",time()); $msg = "$datetime'". sprintf("%06s",intval($usec*1000000)).": $data"; $save_path = 'foo.txt'; $fp = @fopen($save_path, 'a'); // open or create the file for writing and append info fputs($fp, "$msg\n"); // write the data in the opened file fclose($fp); // close the file }
writeLog('Este es el primer log');
$json = '{"users":[{ "user": "Carlos", "age": 30, "country": "Spain" }, { "user": "John", "age": 25, "country": "United States" }]}'; // Si existe la función json_last_error_msg devuelve su valor. En caso contrario si existe json_last_error devuelve el texto que tenemos asociado. Si usamos una versión anterior a PHP 5.3 nos devuelve el mensaje de que no se puede mostrar el error function getJSONError(){ return (function_exists('json_last_error_msg')) ? json_last_error_msg() : ((function_exists('json_last_error')) ? getJSONErrorMsg(json_last_error()) : 'This PHP cannot show the error'); } // A partir del código de error nos muestra un mensaje function getJSONErrorMsg($code){ switch ($code) { case JSON_ERROR_NONE: $msg = 'No errors'; break; case JSON_ERROR_DEPTH: $msg = 'Maximum stack depth exceeded'; break; case JSON_ERROR_STATE_MISMATCH: $msg = 'Underflow or modes mismatch'; break; case JSON_ERROR_CTRL_CHAR: $msg = 'Unexpected control character found'; break; case JSON_ERROR_SYNTAX: $msg = 'Syntax error, malformed JSON'; break; case JSON_ERROR_UTF8: $msg = 'Malformed UTF-8 characters, possibly incorrectly encoded'; break; default: $msg = 'Unknown error'; break; } return $code.' '.$msg; } // Limitamos la profundidad de la recursividad a 2 $obj = json_decode($json, true, 2); var_dump($obj); /* devuelve NULL -> la profundidad es 4 */ if ($obj==null){ $error = getJSONError(); echo $error; /* devuelve 1 Maximum stack depth exceeded */ }
$json = '{"users":[{ "user": "Carlos", "age": 30, "country": "Spain" }, { "user": "John", "age": 25, "country": "United States" }]}'; // Decodificamos la cadena JSON $obj = json_decode($json); var_dump($obj); /* devuelve object(stdClass)#1 (1) { ["users"]=> array(2) { [0]=> object(stdClass)#2 (3) { ["user"]=> string(6) "Carlos" ["age"]=> int(30) ["country"]=> string(5) "Spain" } [1]=> object(stdClass)#3 (3) { ["user"]=> string(4) "John" ["age"]=> int(25) ["country"]=> string(13) "United States" } } } */ // Devolvemos el resultado como array $obj = json_decode($json, true); var_dump($obj); /* devuelve array(1) { ["users"]=> array(2) { [0]=> array(3) { ["user"]=> string(6) "Carlos" ["age"]=> int(30) ["country"]=> string(5) "Spain" } [1]=> array(3) { ["user"]=> string(4) "John" ["age"]=> int(25) ["country"]=> string(13) "United States" } } } */ // Limitamos la profundidad de la recursividad a 2 $obj = json_decode($json, true, 2); var_dump($obj); /* devuelve NULL -> la profundidad es 4 */