0

I have the following PHP Code:

<?php
$file = "Links.txt";
$parts = new SplFileObject($file); // this is your array of words

foreach($parts as $word) {
    $content = file_get_contents($word);
    parse_str($content, $ytarr);
    echo $ytarr['title'];
    unset($content);
}
?>

Please note:

  • The Links.txt file includes multiple external URL's, on each line is only one URL. Example:

www.External-URL-number-ONE.com

www.External-URL-number-TWO.com

www.External-URL-number-THREE.com

  • Each of this URL have the 'title' item in the variable $content (after filling it by "file_get_contents($word);".
  • For troubleshooting purpose, I tested each URL by adding it in the "links.txt" single. The result was for each URL successful. The issue occours, if I add multiple URL's. In that case, the behavior is:

Error message and result:

Notice: Undefined index: title in C:\xampp\htdocs\PHPexample\index.php on line 13

Display the Title of "www.External-URL-number-THREE.com"

How can I fix this problem? It should work also with multiple lines.

Thanks in advance.

EDIT:

The content of the variable $content is:

Array (

[reason] => Invalid parameters.

[status] => fail

[errorcode] => 2

)

Array (

[ISD] => 928398

[enable] => 1

[list] => 39/9339/30

[AMP] => 

[host] =>     

[title] => This_Is_the_Title_Three

[token] => 1

)

UPDATE

I have used the isset() for checking the array before access it. And only the last for each loop have an index.

Baku Bakar
  • 442
  • 2
  • 8
  • 20
  • what is the content of `$content`? I don't understand why parse_str could be the right choice here. – Jeff Nov 29 '18 at 23:37
  • 1
    _"Each of this url have the title item in the content file content"_ - in what form/syntax? – Jeff Nov 29 '18 at 23:39
  • @Jeff $content is the content of the file which is read by accessing the URL. But as mentioned above, it works with each single URL. The error is only appearing if the 'Links.txt' file includes multiple URL's. – Baku Bakar Nov 29 '18 at 23:40
  • @Jeff the content is plain text. – Baku Bakar Nov 29 '18 at 23:41
  • So the content of the downloaded files is literally `title=bar`? – Jeff Nov 29 '18 at 23:44
  • And what is the $content when you do it in a loop and it fails? – Jeff Nov 29 '18 at 23:45
  • Possible duplicate of ["Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined) – miken32 Nov 29 '18 at 23:46
  • @Jeff - It's more like: `[title] => This_Is_the_Title_Three` – Baku Bakar Nov 29 '18 at 23:49
  • then parse_str won't help you there... see this fiddle: https://3v4l.org/XUJWT – Jeff Nov 29 '18 at 23:55
  • So finally we see the real content of content. It's a print_r() output of an array. Good luck in parsing that. Do you control what's returned from those urls? – Jeff Nov 30 '18 at 00:06
  • Please note, that I am a beginner in PHP. So i try my best, to provide you as much as information as I can. Before that, I did first have to find out the code print_r() output... But no, I do not have the control of these urls. – Baku Bakar Nov 30 '18 at 00:10
  • 1
    can you give me one link so that I can have a look at it? – Jeff Nov 30 '18 at 00:21
  • 1
    Yes, to see the structure and encode... the URL in the list.txt have the prefix "http://"? – esdebon Nov 30 '18 at 00:24
  • Here it is: `http://youtube.com/get_video_info?video_id=Bey4XXJAqS8` `http://youtube.com/get_video_info?video_id=7HaJArMDKgI` (please don't tell me to use the Youtube API for solving my problem. :-), thx) – Baku Bakar Nov 30 '18 at 00:30
  • That looks again quite different. Now parse_str() could actually work. You please the content of links.txt – Jeff Nov 30 '18 at 00:38
  • @Jeff the content of 'linnks.txt' is very simple. On the first line its the first link and on the second line the second link. The links are these which I did mention above... – Baku Bakar Nov 30 '18 at 06:51
  • @Jeff do you have any suggestion? – Baku Bakar Nov 30 '18 at 20:21

1 Answers1

0

Read the file and read line by line

<?PHP 
$file = "Links.txt";
$handle = @fopen($file, "r");

if ($handle) {
    // Read line by line
    while (($word = fgets($handle)) !== false) {
        $content = file_get_contents($word);

        // parse_str($content, $ytarr);  // parse_str don't work in this case
        //echo @$ytarr['title'];
        //unset($content);

        echo getDataTag($content, 'title');
    }
    fclose($handle);
}


//This is a dirty solution
function getDataTag($str, $tag){
    $str = str_replace('+',' ',$str);
    $data = explode(($tag.'='),$str);
    $data = explode('&',$data[1]);
    return $data[0];
}

?>
esdebon
  • 2,460
  • 5
  • 26
  • 33
  • Thanks for the answer. But the result of your code is the same. It still does display only the title of the last URL. I would like to display of the title of each URL. – Baku Bakar Nov 30 '18 at 00:08
  • Only the title in the las URL is defined, parse_str don't work in this case, I edited the answer. – esdebon Nov 30 '18 at 18:10
  • not really, because try your code with only one URL. You will see, the title is defined on each URL. Unfortunately your edited answer doesn't work.... Notice: Undefined offset: 1 in.... and also the title is encoded. – Baku Bakar Nov 30 '18 at 20:20
  • Did you test your script with the URLs i have mentioned above in the comments? Was it successsful? – Baku Bakar Dec 02 '18 at 16:27