Nov 14, 2009

PHP Proxy Pass-Through using several keywords

While working on our group project, we had some problems with our PHP Proxy when we handed over keywords, seperated by spaces. The result of the CURL request was an error that Google Translate was not able to process the request.

To solve this problem, we tried it with escape(variable), or manually by exchanging the space by "%20" in the script. After some research we found out that the proxy interprets the "%20" to space. So we had to find a solution within the proxyfile, and here it is:

$rk = $_REQUEST['rk'];
$token = strtok($rk, " "); //tries to split $rk into several parts
// by cutting everywhere where a space is used as delimiter
$test=""; // the new string that will be created
$test1="%20"; // the new delimiter
while ($token != false) //iteration over the parts
{
$test=$test.$token.$test1; // value assignment to $test
$token = strtok(" "); //next part of $rk will be assigned to $token
}
$rk = $test;



We use the PHP function strtok(string,delimiter) do initialize the splitting process. With strtok(delimiter) we will get the next part of the string. This was the only way that allowed us the replacements of spaces with its entity.
I guess some of you could use this.

2 comments:

  1. Jassin,

    Thanks for the post. Currently in my personal project my keyword search isn't as robust as i'd like, and have been meaning to get back to it. So some of this information may help help me improve/refine it some.

    ReplyDelete
  2. After reading your comment about posing a simular problem to my proxy PHP problem. I had to come looking to see what you did. I am having a simular problem with spaces so I will see if I can get it to work by using %20 instead of " ".

    ReplyDelete