A
A
Azat Kiberov2018-04-06 16:53:18
PHP
Azat Kiberov, 2018-04-06 16:53:18

How to avoid Illegal characters found in URL error with curl?

Hello everyone, I've run into a problem.
Wanted to create a way to send urls directly from a text file to curl .
But there was an error Illegal characters found in URL . How to fix?
Here is the code

<?php
echo '<form action="" method="post" >';
echo 'Сайт';
echo '<input class="url" type="text" name="urlsite" size="10" value="';
?>

<?php
$show_info = file('mes.txt');
echo $show_info[0];
?>

<?php
echo '" />';
echo 'Описание';
echo '<input class="text" type="text" name="text" size="10" value="';
?>

<?php
echo $show_info[1];
?>

<?php
echo '" />';
echo '<input type="submit" name="Button" value="добавить" />';
echo '</form>';


if (!empty($_POST)) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $show_info[2]);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, array(
        'urlsite' => $_POST['urlsite'],
        'text' => $_POST['text'],
        'Button' => 'get'
    ));
    curl_setopt($ch, CURLOPT_USERAGENT, 'MSIE 5');
    curl_setopt($ch, CURLOPT_REFERER, "http://ya.ru");
    $res = curl_exec($ch);
    if (!$res) {
        $error = curl_error($ch) . '(' . curl_errno($ch) . ')';
        echo $error;
    } else {
        if (preg_match("/\<table class\='yaResultat'(.+)\<\/table\>/isU", $res, $found)) {
            $content = $found[0];
            echo $content;
        } else {
            echo '<form></form>';
    }
  }
  curl_close($ch);
}

if (!empty($_POST)) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $show_info[3]);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, array(
        'urlsite' => $_POST['urlsite'],
        'text' => $_POST['text'],
        'Button' => 'get'
    ));
    curl_setopt($ch, CURLOPT_USERAGENT, 'MSIE 5');
    curl_setopt($ch, CURLOPT_REFERER, "http://ya.ru");
    $res = curl_exec($ch);
    if (!$res) {
        $error = curl_error($ch) . '(' . curl_errno($ch) . ')';
        echo $error;
    } else {
        if (preg_match("/\<table class\='yaResultat'(.+)\<\/table\>/isU", $res, $found)) {
            $content = $found[0];
            echo $content;
        } else {
            echo '<form></form>';
    }
  }
  curl_close($ch);
}
?>

links are stored inside the txt file
Looks like this
sire.ru
site2.ru
And with the help of this command the code from the first line is read
<?php
echo $show_info[0];
?>
from the second line
<?php
echo $show_info[1];
?>
The bottom line is that when there is only one link in a text file, everything goes well ... And when there are 2 or 3, then there are already errors. Moreover, they are written from a new line.
The following error pops up
Illegal characters found in URL
How to fix it please help?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
Boris Korobkov, 2018-04-06
@mbxngtgo

trim()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question