Replace in cUrl

0

UPDATED URLs

Hi, I want to download an external file and replace some characters and save the data in a file on my server.

Here shows how it does, what I want to do is using cUrl, since the new hosting provider does not allow me to use these functions here, but what if.

<?php

$file = file("http://exabytetv.info/sgeo.m3u");

// Reemplazando
$bodytag = str_replace(' url-tvg="atv"', "",$file);

$cadena_equipo = implode("", $bodytag);

$file = 'lista.m3u';

$current = file_get_contents($file);

$current = $cadena_equipo;

file_put_contents($file, $current);
?>

Here I download a file, but it does not change.

<?php
ob_start();
    $url  = "http://exabytetv.info/sgeo.m3u";

    //El nombre del archivo donde se almacenara los datos descargados.
    $filePath = @fopen("lista.m3u", "w");

    //Inicializa Curl.
    $ch = curl_init();

    //Pasamos la url a donde debe ir.
curl_setopt($ch, CURLOPT_URL, $bodytag);
    //Si necesitamos el header del archivo, en este caso no.
curl_setopt($ch, CURLOPT_HEADER, false);
    //Si necesitamos descargar el archivo.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    //Lee el header y se mueve a la siguiente localización.
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
    //Cantidad de segundo de limite para conectarse.
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
    //Cantidad de segundos de limite para ejecutar curl. 0 significa indefinido.
curl_setopt($ch, CURLOPT_TIMEOUT, 0);
    //Donde almacenaremos el archivo.
curl_setopt($ch, CURLOPT_FILE, $filePath);
    //curl_exec ejecuta el script.
$result = curl_exec($ch);
    //Dejamos de utilizar el archivo creado.
    fclose($fd);
    if($result){ //funciono ?
         echo "Descarga correcta.";
    // header('Location: convertir-local.php');
     }
?>

I want to download external file, read it, replace some characters and save it. Thank you in advance.

    
asked by Jhossep Lincohl Vargas Conde 06.04.2017 в 14:18
source

1 answer

0

It seems that your error is that you were not properly configuring the URL in CURLOPT_URL :

<?php
ob_start();
$url  = "http://hola.com/sgeo.m3u";

//Inicializa Curl.
$ch = curl_init();

//Pasamos la url a donde debe ir.
curl_setopt($ch, CURLOPT_URL, $url);

//Si necesitamos el header del archivo, en este caso no.
curl_setopt($ch, CURLOPT_HEADER, false);
//Si necesitamos descargar el archivo.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//Lee el header y se mueve a la siguiente localización.
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
//Cantidad de segundo de limite para conectarse.
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
//Cantidad de segundos de limite para ejecutar curl. 0 significa indefinido.
curl_setopt($ch, CURLOPT_TIMEOUT, 0);

//curl_exec ejecuta el script.
$result = curl_exec($ch);
if ($result !== false) { //funciono ?
 echo "Descarga correcta.";
 $result = str_replace(' url-tvg="atv"', "",$result);
 file_put_contents("lista.m3u", $result);
}

By the way, if you use CURLOPT_RETURNTRANSFER you do not need to assign an output file. The output of curl_exec() will be the content downloaded and, therefore, you can use file_put_contents() to save it easily.

    
answered by 06.04.2017 / 14:57
source