forumnz Posted May 11, 2007 Share Posted May 11, 2007 Does anyone know how it may be possible to write some code to search multiple search engines? Please help. Sam. Quote Link to comment https://forums.phpfreaks.com/topic/50981-search-multiple-search-engines/ Share on other sites More sharing options...
chigley Posted May 11, 2007 Share Posted May 11, 2007 This isn't very difficult. What do you plan to do with the search results after you obtain them? Quote Link to comment https://forums.phpfreaks.com/topic/50981-search-multiple-search-engines/#findComment-250815 Share on other sites More sharing options...
forumnz Posted May 11, 2007 Author Share Posted May 11, 2007 Display them on a single page What would it take? Quote Link to comment https://forums.phpfreaks.com/topic/50981-search-multiple-search-engines/#findComment-250818 Share on other sites More sharing options...
forumnz Posted May 11, 2007 Author Share Posted May 11, 2007 Or multiple pages - depend how many results Quote Link to comment https://forums.phpfreaks.com/topic/50981-search-multiple-search-engines/#findComment-250825 Share on other sites More sharing options...
chigley Posted May 11, 2007 Share Posted May 11, 2007 If I was doing it I'd do something like this: <?php $links = array("http://www.google.co.uk/search?q={QUERY}", "http://search.live.com/results.aspx?q={QUERY}"); $query = $_GET['query']; // Contains the value of what you want to search for foreach($links as $url) { $url = str_replace("{QUERY}", $query, $url); $content = file_get_contents($url); echo "$content\n\n"; } ?> You'd then go on to parse $content reading the bits of data you want. Quote Link to comment https://forums.phpfreaks.com/topic/50981-search-multiple-search-engines/#findComment-250828 Share on other sites More sharing options...
forumnz Posted May 11, 2007 Author Share Posted May 11, 2007 Gulp... so i need to make a form that sends data to {query} or something and i also need an output page? Quote Link to comment https://forums.phpfreaks.com/topic/50981-search-multiple-search-engines/#findComment-250838 Share on other sites More sharing options...
chigley Posted May 11, 2007 Share Posted May 11, 2007 Well at the moment that code takes the query (what you want to search for) and searches for it in each search engine found in the $links array. In the $links array, you need to insert all the search engine urls, placing {QUERY} where the search query goes. I worked out the links for Google and Live already. So you may do this: index.php: <form action="search.php" method="post">Search for: <input type="text" name="query" /></form> and alter the PHP code to: search.php: <?php $links = array("http://www.google.co.uk/search?q={QUERY}", "http://search.live.com/results.aspx?q={QUERY}"); $query = $_POST['query']; // Contains the value of what you want to search for foreach($links as $url) { $url = str_replace("{QUERY}", $query, $url); $content = file_get_contents($url); echo "$content\n\n"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/50981-search-multiple-search-engines/#findComment-250842 Share on other sites More sharing options...
forumnz Posted May 11, 2007 Author Share Posted May 11, 2007 Thanks, it kinda worked, but not as I hoped - this is what happend - result Quote Link to comment https://forums.phpfreaks.com/topic/50981-search-multiple-search-engines/#findComment-250853 Share on other sites More sharing options...
chigley Posted May 11, 2007 Share Posted May 11, 2007 Well now you have the variable, $content, containing the source code of the result page of each search engine. Now you need to start parsing the HTML output of the page to fish out the bits of info you want. Also, instead of going to http://www.designervision.co.nz/search/search.php, go to http://www.designervision.co.nz/search/index.php and type in your query and hit the return key. Look up HTML parsing and see what you can find! Quote Link to comment https://forums.phpfreaks.com/topic/50981-search-multiple-search-engines/#findComment-250860 Share on other sites More sharing options...
forumnz Posted May 11, 2007 Author Share Posted May 11, 2007 Ok thanks a lot, will do! Sam. Quote Link to comment https://forums.phpfreaks.com/topic/50981-search-multiple-search-engines/#findComment-250865 Share on other sites More sharing options...
forumnz Posted May 11, 2007 Author Share Posted May 11, 2007 Oh dear, this seems a just a little bit to advanced for me - only a little (you could say im an advanced noob in php). Would I need to make a new page to display the results? Quote Link to comment https://forums.phpfreaks.com/topic/50981-search-multiple-search-engines/#findComment-250872 Share on other sites More sharing options...
chigley Posted May 11, 2007 Share Posted May 11, 2007 If I were doing it I'd start with Live search because its results are easier to parse. I don't want to tell you all the code because it doesn't help you learn at all. This function is useful: <?php function textbetweenarray($s1,$s2,$s){ $myarray=array(); $s1=strtolower($s1); $s2=strtolower($s2); $L1=strlen($s1); $L2=strlen($s2); $scheck=strtolower($s); do{ $pos1 = strpos($scheck,$s1); if($pos1!==false){ $pos2 = strpos(substr($scheck,$pos1+$L1),$s2); if($pos2!==false){ $myarray[]=substr($s,$pos1+$L1,$pos2); $s=substr($s,$pos1+$L1+$pos2+$L2); $scheck=strtolower($s); } } } while (($pos1!==false)and($pos2!==false)); return $myarray; } ?> Usage: <?php $uls = textbetweenarray("<ul class=\"metaData\">", "</ul>", $content); ?> Then break each <ul> into smaller chunks! [edit] Remember that the first <ul> has a class attatched to it! [/edit] Quote Link to comment https://forums.phpfreaks.com/topic/50981-search-multiple-search-engines/#findComment-250879 Share on other sites More sharing options...
forumnz Posted May 11, 2007 Author Share Posted May 11, 2007 Ok, I understand most of the code there - thanks. But, I don't know what to do with it. Help? Sorry and thanks. Quote Link to comment https://forums.phpfreaks.com/topic/50981-search-multiple-search-engines/#findComment-250884 Share on other sites More sharing options...
forumnz Posted May 11, 2007 Author Share Posted May 11, 2007 Can someone plese help me? Quote Link to comment https://forums.phpfreaks.com/topic/50981-search-multiple-search-engines/#findComment-250902 Share on other sites More sharing options...
john010117 Posted May 11, 2007 Share Posted May 11, 2007 Please wait at least an hour before double posting. Also, I suggest you look up read about some of the functions (that's been included in the custom functions) on php.net Quote Link to comment https://forums.phpfreaks.com/topic/50981-search-multiple-search-engines/#findComment-250917 Share on other sites More sharing options...
chigley Posted May 12, 2007 Share Posted May 12, 2007 Yeah I think you have enough to work with now. textbetweenarray() simply makes an array with everything between two points. The Live search results are done in list format, which is easier to parse as in my example above. Have a go at doing it yourself Quote Link to comment https://forums.phpfreaks.com/topic/50981-search-multiple-search-engines/#findComment-251183 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.