简体   繁体   中英

cURL not handling foreign http characters php

I have in a string an url which has unfortunately foreign characters . when I send them to curl_setopt ($go, CURLOPT_URL, $url) nothing happens, the command is not running. On the other hand when I use my chrome browser and copy the link location , which has foreign characters and paste them on the browser suddendly the the foreign chars are encoded to something like %20%D5%F0%EF%EB%EF%E3%E9%F3%F4%DD%F2 , I know that %20 stands for space .. and this converted string works with curl command Have used urlencode , iconv but haven't find the right method yet. the result doesnt match with the pasted one. Is there any function which does it? The chars are greek. Thanks a lot

I had the same problem.

When I was running the curl functions and the url contained only English characters everything was fine.

But when the url contained Greek characters then it didn't work and returned "HTTP Error 400. The request URL is invalid."

I searched a lot and the solution was to use the following command:

$url = mb_convert_encoding($url, "iso-8859-7", "UTF-8");

Below is a full example:

$ch = curl_init(); 
$url = mb_convert_encoding($url, "iso-8859-7", "UTF-8");
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
curl_setopt($ch, CURLOPT_HEADER, true);
$page = curl_exec($ch); 
curl_close($ch);

What is original encoding of that webpage? Can you give us url and part of source code?

Try to convert string to requested character encoding

$response = html_entity_decode(htmlentities(curl_exec($ch)), ENT_COMPAT, 'UTF-8'); 
$response = iconv('windows-1253','utf-8',$response );  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM