Help me with XML and PHP

Started on the old forum in April 2011 · view the original in the archive · old links redirect here

#xml #xml-parser

12 replies · 1,665 views · started 15 years ago · 1 min read

6 participants
Aan Damar Newbie · 7 posts #1

It has been 3 days since I trying to learn about PHP. I sleep for 3 hours only a day, I can't sleep till I realizing it. I absolutely don't know PHP but I want to realized this code.

hopefully there someone who can help me so I can sleep tight tonight. this is my big problem:

<?php
$request_url = "http://search.4shared.com/network/searchXml.jsp?q='.urlencode($termstring).'&searchExtention=mp3&sortType=1&sortOrder=1&searchmode=3&start=1";
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $request_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3 (.NET CLR 3.5.30729) (Prevx 3.0.5)');
$data = curl_exec($ch);
curl_close($ch);

$xml = SimpleXMLElement($data);
$name = $xml->getElementsByTagName('name')->item(0)->nodeValue;
$date = $xml->getElementsByTagName('upload-date')->item(0)->nodeValue;
$altr = $xml->getElementsByTagName('url')->item(0)->nodeValue;
$donlod = $xml->getElementsByTagName('flash-preview-url')->item(0)->nodeValue;
$ukrn = $xml->getElementsByTagName('size')->item(0)->nodeValue;
$oleh = $xml->getElementsByTagName('user')->item(0)->nodeValue;
?>

I want to extract the 4shared xml search result into php. there is no result yet for it. I try to implement code for showing result using foreach, but still error.

I hope you can help me.

Thanks!

GumSlone Administrator Principal · 1,690 posts #2
<?php
$termstring = 'muz';
$request_url = 'http://search.4shared.com/network/searchXml.jsp?q='.urlencode($termstring).'&searchExtention=mp3&sortType=1&sortOrder=1&searchmode=3&start=1';
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $request_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3 (.NET CLR 3.5.30729) (Prevx 3.0.5)');
$data = curl_exec($ch);
curl_close($ch);

$xml = simplexml_load_string($data);
$json = json_encode($xml);
$arr = json_decode($json,true);
#print_r($arr);
foreach($arr['result-files']['file'] AS $file)
{
	echo '<a href="'.$file['flash-preview-url'].'">'.$file['name'].'</a> '.$file['size'].'<br/>';

}

?>

Advertise your mobile site for FREE with AdTwirl

External image (may be gone)

Aan Damar Newbie · 7 posts #3

coding-talk.com rocks!

Trying to implement it. Thank you very much admin. I'll post again when getting error 😀 I hope you can answer my stupid question 🙂

thank you very much. coding-talk.com rocks!

Added after 14 minutes:

you rocks admin! I implement it and get it works! Words isn't enough to say thanks and happy that I feel now.

this is the code that I've implemented in my search.php file. I'm using wordpress btw.

<?php
$termstring = $s;
$request_url = 'http://search.4shared.com/network/searchXml.jsp?q='.urlencode($termstring).'&searchExtention=mp3&sortType=1&sortOrder=1&searchmode=3&start=1';
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $request_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3 (.NET CLR 3.5.30729) (Prevx 3.0.5)');
$data = curl_exec($ch);
curl_close($ch);

$xml = simplexml_load_string($data);
$json = json_encode($xml);
$arr = json_decode($json,true);
#print_r($arr);
?>

<?php if ($s!='') {
foreach($arr['result-files']['file'] AS $file) {
	echo '<a href="'.$file['flash-preview-url'].'">'.$file['name'].'</a> '.$file['size'].'<br/>';
}
}
?>

Thanks admin! You inspiring me. I'll learn more things so I can smart like you.

Next thing must I learn: adding pagination rename the preview when user download the file. steal other site player 😀, clone it and implement it.

Thanks admin You rocks!

Aan Damar Newbie · 7 posts #4

This is my first code that works with admin help. I love it and want develop it. since I'm totally new and has no coding background, I hope admin and all friends in Coding Talk can help me develop it 🙂 I plan to adding pagination and player. I cancel plan to rename the file automatically. It seems server must download the file before it can be renamed.

After implement it, I adding some function to remove not useful characters including _ and mp3. It seems runs good. But, I found other problem. It getting error if there is no result found.

I'm trying to fix it. I'm looking at 4shared xml file and find the different that is total files = 0

<total-files>0</total-files>

and this is what I've typed:

<?php if ($s!='') {
echo '<table width="100%">';
echo '<tr><th width="60%">Name</th><th>Size</th><th>Download</th></tr>';
$counter = $arr['search-result']['total-files'];
$file = $arr['result-files']['file'];
if ($counter = 0 ) {
echo '<tr><td>Sorry, there is no result</td></tr>';
}
else {
foreach($file AS $files) {
echo '<tr>';
echo '<td>'.cleaning(delete_mp3($files['name'])).'</td>';
echo '<td>'.$files['size'].'</td>';
echo '<td><div class="download"><a onclick="window.open (''.$files['flash-preview-url'].'')" href="#" rel="nofollow" href="">Download</a></div></td>';
echo '</tr>';
}
}
echo '</table>';
}
?>

Unfortunately, I still get error when no result found. admin and all friends in Coding Talk, can you help me please where is the error and how I can fix it.

thank you very much admin and all friends in Coding talk

ps: I'm very sorry for my very bad english

Aan Damar Newbie · 7 posts #6

it show message like this sir: Warning: Invalid argument supplied for foreach() in /home/xxxxxxx/public_html/wp-content/themes/mp3/search.php on line 56

do you need the login sir? thanks

Aan Damar Newbie · 7 posts #8

something else wrote: $file is empty making foreach invalid

thanks sir. It very useful, but I still can't implement it.

this 2 kinds code I try to implement, but still see the error 🙁

first code I try using empty $file. Still error 😀

<?php
$file = $arr['result-files']['file'];
if ($s!='') {
echo '<table width="100%">';
echo '<tr><th width="60%">Name</th><th>Size</th><th>Download</th></tr>';
foreach($file AS $files) {
echo '<tr>';
echo '<td>'.cleaning(delete_mp3($files['name'])).'</td>';
echo '<td>'.$files['size'].'</td>';
echo '<td><div class="download"><a href="'. get_settings('home').'/dl?='.base64_encode($files['flash-preview-url']).'" rel="nofollow" href="">Download</a></div></td>';
echo '</tr>';
}
if (empty($file)) {
echo '<tr><td>Zero results</td></tr>';
}
echo '</table>';
}
?>

Second code

<?php
$file = $arr['result-files']['file'];
if ($s!='') {
echo '<table width="100%">';
echo '<tr><th width="60%">Name</th><th>Size</th><th>Download</th></tr>';
if ($file == 0) {
echo '<tr><td>zero results..</td></tr>';
}
else {
foreach($file AS $files) {
echo '<tr>';
echo '<td>'.cleaning(delete_mp3($files['name'])).'</td>';
echo '<td>'.$files['size'].'</td>';
echo '<td><div class="download"><a href="'. get_settings('home').'/dl?='.base64_encode($files['flash-preview-url']).'" rel="nofollow" href="">Download</a></div></td>';
echo '</tr>';
}
}
echo '</table>';
}
?>

How I can inform it when there is no result found? I see total-files is 0 when there is no result found.

http://search.4shared.com/network/searchXml.jsp?q=bon+jovi&searchExtention=mp3&sortType=1&sortOrder=1&searchmode=3&start=1

result

<!--<?xml version="1.0" encoding="UTF-8"?>-->
<search-result>
<query>bon jovi</query>
<total-files>0</total-files>
<page-number>1</page-number>
<pages-total>0</pages-total>
<start>1</start>
<files-per-page>10</files-per-page>
<files-approx-count>0</files-approx-count>
</search-result>

May I use this parameter to inform the script if there is no result? or I must using other parameter?

I'm really confused, but it make me addicted lol

something else Fellow · 2,296 posts #9

i think you need to change this line: if ($file == 0) { to something like: if (count($file) == 0) {

Aan Damar Newbie · 7 posts #10

Success!!!!!

thank you very much sir something else. you're the another Rocks people!

thank you very much!

Added after 7 minutes:

next chapter, adding pagination 🙂 always need helps from you all of friends here 😉

thanks Coding Talk

babahe Newbie · 2 posts #11

Hai Damar, how to adding pagination ?? plz..help me...thanks

CreativityKills Lead · 833 posts #12

i dnt believe in pampering yung coders like my brethrens Gum and something else, so i'll tell u something else... GO and READ. Stop cheaply coupling up a 4shared grabber.

Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close