Problem when shortening multiple URLs with TinyURL

2

I want to get several URLs shortened simultaneously, the next problem is that when I shorten them, the first URL is shortened correctly, but the others are shown like this: tinyurl.com/__urlqueacortas.url and I get an error because it is a wrong address.

Below I put the used code:

  

Note: tinyurl . com appears separately, but it is not an error. It's because the Stack Overflow editor will not let me put it together if I want to post the question.

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet">
<form action="" method="post">
<textarea rows="20" name="urls" cols="40"><?= $_POST['urls'] ?></textarea>
    <input id="submit" type="submit" name="submit" value="Acortar" class="btn-glow primary">
</form>

<?php

$enlace = $_POST['urls'];
$enlace2 = chop($enlace,'__');    // Elimina saltos de linea y espacio, pero solo al final de la cadena 
$enlace3 = nl2br($enlace2);         // Agregamos los saltos de linea <br /> 
$enlace4 = explode("<br />", $enlace3);    // Creamos array con los datos recibidos 

function acortar($link) {
    $enlace5 = file_get_contents('http://tinyurl . com/api-create.php?url='.$link.''.$key.'');
    return $enlace5;
}

foreach($enlace4 as $link){
    echo acortar($link);
    echo "<br>";
}
    
asked by Cesar Rz 07.07.2017 в 04:20
source

1 answer

0

Well I tried your algorithm and found the problem, but we go for part: First there is no need to do this chop, I show you in the version that I have a better way to do it.

$enlace2 = chop($enlace,'__'); 

The problem is that here when adding the lines for jumps are not replacing if you are adding the car jump before, and you do not need it since the txt area is sending them the / n then to than trying to transform them

$enlace3 = nl2br($enlace2); 

Then finally I recommend you to try this api link make a wrapper of all the shortening services and show messages of error when you can not shorten it easier to handle later.

I have attached your corrected code, add some isset to the post to be able to test it and also leave the function that you had and write shorten_api in which I use curl and the aforementioned api.

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet">
<form action="" method="post">
<textarea rows="20" name="urls" cols="40"><?= isset($_POST['urls'])?$_POST['urls']:'' ?></textarea>
    <input id="submit" type="submit" name="submit" value="Acortar" class="btn-glow primary">
</form>

<?php

if(isset($_POST['urls'])){
$enlace = $_POST['urls'];
$enlace2 = str_replace(' ', '', $enlace);    // Elimina espacios blancos innecesarios pero no el salto de carro 
$enlace3 = explode("\n", $enlace2);    // Creamos array con los datos recibidos //FILTRANDO por el salto de carro que ya viene

foreach($enlace3 as $link){
    echo acortar_normal($link);
    echo "<br>";
}


}

function acortar_normal($link) {
    $key = "TUKEYAUI";
    $enlace5 = file_get_contents('http://tinyurl . com/api-create.php?url='.$link.''.$key.'');
    return $enlace5;
}

function acortar_api($link) {
    $key = "TUKEYAUI";
    $curl = curl_init(); 
    $post_data = array('format' => 'json',
                       'apikey' => $key,
                       'provider' => 'tinyurl_com',
                       'url' => $link );
    $api_url = 'http://tiny-url.info/api/v1/create';
    curl_setopt($curl, CURLOPT_URL, $api_url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_POST, 1);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
    $resultado = curl_exec($curl);
    curl_close($curl);

    return $resultado;

}

Try it and tell me how.

Greetings!

    
answered by 07.07.2017 в 20:49