Roger Stringer   About ▾

I'm Roger Stringer: a father, writer, developer, consultant, chef, speaker. Founder of TheInterviewr.

Monthly Archives: November 2006


Overture Keyword Extractor using PHP

 

After looking around for a class that would extract keywords from the Overture Keyword Inventory database, I decided it was just as well to do up my own class. You can use this to look up keywords and view the results. Hope you find it useful.

<span style="color: #000000;"><code>01</span> </code>
<span style="color: #000000;">02</span> <span style="color: #0000bb;"><?</span>
<span style="color: #000000;">03</span> <span style="color: #007700;">    class </span><span style="color: #0000bb;">OvertureSuggest </span><span style="color: #007700;">{</span>
<span style="color: #000000;">04</span> <span style="color: #007700;">        private </span><span style="color: #0000bb;">$BaseWords</span><span style="color: #007700;">;</span>
<span style="color: #000000;">05</span> <span style="color: #007700;">        </span><span style="color: #ff8000;">/** constructor */</span>
<span style="color: #000000;">06</span> <span style="color: #007700;">        function </span><span style="color: #0000bb;">__construct</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$BaseWords</span><span style="color: #007700;">) {</span>
<span style="color: #000000;">07</span> <span style="color: #007700;">            </span><span style="color: #0000bb;">$this</span><span style="color: #007700;">-></span><span style="color: #0000bb;">BaseWords </span><span style="color: #007700;">= </span><span style="color: #0000bb;">$BaseWords</span><span style="color: #007700;">;</span>
<span style="color: #000000;">08</span> <span style="color: #007700;">        }</span>
<span style="color: #000000;">09</span> <span style="color: #007700;">        function </span><span style="color: #0000bb;">getSuggestions</span><span style="color: #007700;">() {</span>
<span style="color: #000000;">10</span> <span style="color: #007700;">            </span><span style="color: #0000bb;">$query </span><span style="color: #007700;">= </span><span style="color: #dd0000;">'http://inventory.overture.com/d/searchinventory/suggestion/'</span><span style="color: #007700;">;</span>
<span style="color: #000000;">11</span> <span style="color: #007700;">            </span><span style="color: #0000bb;">$query </span><span style="color: #007700;">.= </span><span style="color: #dd0000;">'?term='</span><span style="color: #007700;">.</span><span style="color: #0000bb;">urlencode</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$this</span><span style="color: #007700;">-></span><span style="color: #0000bb;">mBaseWords</span><span style="color: #007700;">).</span><span style="color: #dd0000;">'&mkt=us&lang=en_US'</span><span style="color: #007700;">;</span>
<span style="color: #000000;">12</span> <span style="color: #007700;">            </span><span style="color: #0000bb;">$data </span><span style="color: #007700;">= </span><span style="color: #0000bb;">file_get_contents</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$query</span><span style="color: #007700;">);</span>
<span style="color: #000000;">13</span> <span style="color: #007700;">            </span><span style="color: #0000bb;">$data </span><span style="color: #007700;">= </span><span style="color: #0000bb;">strip_tags</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$data</span><span style="color: #007700;">);</span>
<span style="color: #000000;">14</span> <span style="color: #007700;">            </span><span style="color: #0000bb;">$pattern </span><span style="color: #007700;">= </span><span style="color: #dd0000;">"/ ([0-9]+)n (.+)/i"</span><span style="color: #007700;">;</span>
<span style="color: #000000;">15</span> <span style="color: #007700;">            </span><span style="color: #0000bb;">preg_match_all</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$pattern</span><span style="color: #007700;">,</span><span style="color: #0000bb;">$data</span><span style="color: #007700;">,</span><span style="color: #0000bb;">$Matches</span><span style="color: #007700;">);</span>
<span style="color: #000000;">16</span> <span style="color: #007700;">            </span>
<span style="color: #000000;">17</span> <span style="color: #007700;">            </span><span style="color: #0000bb;">$Cnts </span><span style="color: #007700;">= </span><span style="color: #0000bb;">$Matches</span><span style="color: #007700;">[</span><span style="color: #0000bb;">1</span><span style="color: #007700;">];</span>
<span style="color: #000000;">18</span> <span style="color: #007700;">            </span><span style="color: #0000bb;">$Words </span><span style="color: #007700;">= array();</span>
<span style="color: #000000;">19</span> <span style="color: #007700;">            for (</span><span style="color: #0000bb;">$i</span><span style="color: #007700;">=</span><span style="color: #0000bb;">0</span><span style="color: #007700;">;</span><span style="color: #0000bb;">$i</span><span style="color: #007700;"><</span><span style="color: #0000bb;">count</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$Matches</span><span style="color: #007700;">[</span><span style="color: #0000bb;">2</span><span style="color: #007700;">]);</span><span style="color: #0000bb;">$i</span><span style="color: #007700;">++) {</span>
<span style="color: #000000;">20</span> <span style="color: #007700;">                </span><span style="color: #0000bb;">$Words</span><span style="color: #007700;">[</span><span style="color: #0000bb;">$Matches</span><span style="color: #007700;">[</span><span style="color: #0000bb;">2</span><span style="color: #007700;">][</span><span style="color: #0000bb;">$i</span><span style="color: #007700;">]] = </span><span style="color: #0000bb;">intval</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$Cnts</span><span style="color: #007700;">[</span><span style="color: #0000bb;">$i</span><span style="color: #007700;">]);</span>
<span style="color: #000000;">21</span> <span style="color: #007700;">            }</span>
<span style="color: #000000;">22</span> <span style="color: #007700;">            return </span><span style="color: #0000bb;">$Words</span><span style="color: #007700;">;</span>
<span style="color: #000000;">23</span> <span style="color: #007700;">        }</span>
<span style="color: #000000;">24</span> <span style="color: #007700;">    }</span>
<span style="color: #000000;">25</span> <span style="color: #007700;">    </span>
<span style="color: #000000;">26</span> <span style="color: #007700;">    </span><span style="color: #ff8000;">// Sample Usage:</span>
<span style="color: #000000;">27</span>     $suggest = new OvertureSuggest('Affiliate');
<span style="color: #000000;">28</span>     echo "<pre>".print_r ($suggest->getSuggestions(),true)."</pre>";
<span style="color: #000000;">29</span>     exit;    
<span style="color: #000000;">30</span> <span style="color: #0000bb;">?></span>
<span style="color: #000000;">31</span>
</code>

Filed Under: Articles

~•~