views:

410

answers:

1

Hi!

I'm using PHP in order to redirect some search query. There is some example of code here (click).

And my PHP code is:

audio_action.php :

<?php
$search_field = trim($_POST['audio_field']);
$search_engine = trim($_POST['audio']);
$url_params = preg_replace('/(\ )+/', '+', $search_field);
$url = array('deezer'=>'http://www.deezer.com/s.php?s=', 'jiwa'=>'http://www.jiwa.fm/#search/track/', 'last.fm'=>'http://www.last.fm/music?q=');
header('Location:'.$url[$_POST['audio']].$url_params)
?>

and video_action.php :

<?php
$search_field = trim($_POST['video_field']);
$search_engine = trim($_POST['video']);
$url_params = preg_replace('/(\ )+/', '+', $search_field);
$url = array('youtube'=>'http://www.youtube.com/results?search_type=&amp;amp;search_query=', 'dailymotion'=>'http://www.dailymotion.com/relevance/search/', 'google_video'=>'http://video.google.com/videosearch?q=');
header('Location:'.$url[$_POST['video']].$url_params)
?>

The problem is that I can't use it when search terms must be in the middle of an url.

For example, for Jiwa it should be: http://www.jiwa.fm/#search/track/{%22q%22%3A%22keywords%22}

Where "keywords" is the place where keywords should be.

And without those %22} characters search doesn't work.

So how to improve this PHP code in order to make it work with such query?

Somebody told me too that $search_engine = trim($_POST['video']); is useless, but when I remove it, it doesn't work anymore.

I'm currently using video_action.php for video search and audio_action.php for audio but if you find some way to merge those file into a single one while keeping 2 forms in my HTML code it would be awesome.

Please help me improve this code.

PS: I don't want to use JavaScript for this.

+2  A: 

Try this:

<?php
if (!empty($_REQUEST['audio_field']))
{
    $url = array(
     'deezer'=>'http://www.deezer.com/s.php?s=__keywords__',
     'jiwa'=>'http://www.jiwa.fm/#search/track/{%22q%22%3A%22__keywords__%22}',
     'last.fm'=>'http://www.last.fm/music?q=__keywords__');

    header('Location:'.str_replace('__keywords__',preg_replace('/(\ )+/', '+', trim($_REQUEST['audio_field'])),$url[trim($_REQUEST['audio'])]));
    die();
}
else if (!empty($_REQUEST['video_field']))
{
    $url = array(
     'youtube'=>'http://www.youtube.com/results?search_query=__keywords__',
     'dailymotion'=>'http://www.dailymotion.com/relevance/search/__keywords__',
     'google_video'=>'http://video.google.com/videosearch?q=__keywords__');

    header('Location:'.str_replace('__keywords__',preg_replace('/(\ )+/', '+', trim($_REQUEST['video_field'])),$url[trim($_REQUEST['video'])]));
    die();
}
else
{
    // No search query; redirect to search page
    header('Location:http://lostsymphonia.free.fr/r/index.html');
    die();
}
?>

Notes:

  1. The two PHP files can now be merged.
  2. Don't use "&amp;" in a URL - that's only for HTML. Location: is an HTTP header, which is not HTML.
  3. Use method="get" instead of method="post" - searching is an idempotent action.
  4. You accidentally used $url[$_POST['audio']] instead of $url[$search_engine] - you never even use $search_engine; how could removing it possibly make a difference?
Zarel
Thanks for your fast answer :-)You were right removing $search_engine doesn't change anything in my old code. I should have remove some bad brackets or so when I did the test.I updated my code and there is some error:Parse error: syntax error, unexpected ';' in /mnt/102/sdb/c/4/lostsymphonia/r/action.php on line 11 which is:header('Location:'.str_replace('__keywords__',$url_params,$url[$search_engine]);Don't you think it's coming from $url[$search_engine] ?Or:$search_engine = trim($_REQUEST['audio']); and ['video]?Why did you decide to keep it? (PS:Sorry can't do line breaks here)
Mark
I forgot an `)`. I've added it now - my current code should work. I decided to keep `$search_engine` because it looks nicer than `$_REQUEST['video']`.
Zarel
Thanks, I updated my code here: http://lostsymphonia.free.fr/r/index.html it does work for audio search but not for video. My full code has more search engines and forms so I would like to make it lighter. Does it really need $search_engine part?
Mark
Whoops, I fixed that, too; I accidentally typed `audio_field` when I meant `video_field`. Making it "lighter" will make it harder to maintain, although with simple code like this it isn't a big deal, so I've just put it all on one line.
Zarel
Thanks that solved the problem. Answer accepted!
Mark