user2981725
user2981725

Reputation: 7

Php - url/string contains arabic chars

I am working on something that grabs 1 image from bing images.
For some reason file_get_contents did not work so I searched a bit and got the following method - which works great with English keywords:

$fp = fsockopen("www.bing.com", 80, $errn, $errs);
$ar = "عربي"; 
$out  = "GET /images/search?q=$ar HTTP/1.1\r\n";
echo "$out";
echo "<Br><br>";
$out .= "Host: www.bing.com\r\n";
$out .= "User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:15.0) Gecko/20100101 Firefox/15.0\r\n";
$out .= "Connection: close\r\n";
$out .= "\r\n";

fwrite($fp, $out);

$response = "";
while ($line = fread($fp, 4096)) {
   $response .= $line;
} 
fclose($fp);


$response_body = substr($response, strpos($response, "\r\n\r\n") + 4);
// or
list($response_headers, $response_body) = explode("\r\n\r\n", $response, 2);

So if $ar contains english keyword(s), it works perfect. However, when I try and put in an arabic word - the results are convoluted - and doesn't match the results to bing image search.

Top of my php file I have :

<meta http-equiv='content-Type' content='text/html; charset=UTF-8'/>

Any help would greatly be appreciated. TIA

Upvotes: 0

Views: 1821

Answers (2)

taco
taco

Reputation: 1383

I know you said you tried urlencode() but this worked for me:

$ar = urlencode("عربي");

Upvotes: 3

m1k1o
m1k1o

Reputation: 2364

I tried to visit that page in browser, with english keyword. Works perfectly.

http://www.bing.com/images/search/?q=dog

When i try it with your string, i get 404 respose:

http://www.bing.com/images/search/?q=عربي

I encoded your string using urlencode, and Works again, prefeclty.

http://www.bing.com/images/search/?q=%D8%B9%D8%B1%D8%A8%D9%8A%0A

Answer -> use definitely urlencode($ar), it is solution of your problem.

Upvotes: 1

Related Questions