0

Currently I am using this code.

$html = file_get_html($url);
$file = $html->find('audio'); 
$data['download'] = $file->attr['data-file'];

to try and gather the data from here.

<div class="actions clearfix">
  <div class="player clearfix">
      <div class="player-init preload" data-file="http://example.com/mp3embed-cegoq1gcnrj6.mp3"></div>
  </div>
 </div>

My code from above is not working, for some reason.

nerdlyist
  • 2,842
  • 2
  • 20
  • 32
Harrison
  • 99
  • 6
  • You'll need something that parses HTML pages. An example is PHP's DOMDocument: http://php.net/manual/en/class.domdocument.php – Daan Oct 17 '18 at 12:13
  • @Daan Sorry, forgot to mention that the above segment is using simple_html_dom http://simplehtmldom.sourceforge.net/ – Harrison Oct 17 '18 at 12:14
  • try and debug `print_r($file);` you need to describe "not working" a lot betterand look at the output. I am fairly certain that `find` is returning an array so you would need something like `$file[0]->attr['data-file'];` – nerdlyist Oct 17 '18 at 12:14
  • Looks like a typo. You are searching for ` – Quentin Oct 17 '18 at 12:15
  • There is no `audio` in the shown example – Cid Oct 17 '18 at 12:15
  • Possible duplicate of [How to use file\_get\_contents or file\_get\_html?](https://stackoverflow.com/questions/14962359/how-to-use-file-get-contents-or-file-get-html) – nerdlyist Oct 17 '18 at 12:15
  • Please enable proper PHP error reporting first of all, if you have not done so yet. (And “not working, for some reason” doesn’t sound like it.) – misorude Oct 17 '18 at 12:16
  • The code in the question seems so random. You're trying to find and `audio` tag which does not exist in the sample HTML you're providing, then you're trying to call a non-existent method `attr` on `null`, because `$file` is null, and even if it wasn't, [there is no such method `attr` in simple html dom library](http://simplehtmldom.sourceforge.net/manual_api.htm). Unfortunately this question shows absolutely no efforts for solving the problem. – Nima Oct 17 '18 at 13:07
  • I don't want an answer then why you are asking I have given you an answer then please check it works for you or not? – Yagnik Detroja Oct 18 '18 at 05:06

2 Answers2

0

May be Something like this will work.

$DOM = new DOMDocument;
$DOM->loadHTML($html);
$xpath = new DOMXpath($DOM);
$items = $xpath->query("//div[contains(@class,'player-init')]");
$mydata = $items->documentElement->getAttribute('data-file');
0

Please tried below code.

$url = 'https://somedomain.com/somesite/';
$content = file_get_contents($url);
$first_step = explode( '<div class="player-init preload" data-file="' , $content );
$data['download'] = explode('"></div>' , $first_step[1] );
echo $data['download'];
exit;
Yagnik Detroja
  • 921
  • 1
  • 7
  • 22