PHP Curl post with non-english characters

Status
Not open for further replies.

t3od0r

Active Member
1,419
2008
183
10
I have a small problem with php curl post.

I am trying to post some turkish characters to a forum but aren't posted how it should be.

This is how i save the text:
PHP:
fwrite($fpp,"\xEF\xBB\xBF");
fwrite($fpp, $row['template']);
fclose($fpp);
and posting:
PHP:
$this->curl = curl_init();
curl_setopt ( $this->curl, CURLOPT_URL, $this->vb_url . 'newthread.php?' . $url_vars );
curl_setopt ( $this->curl, CURLOPT_POST, true );
curl_setopt ( $this->curl, CURLOPT_POSTFIELDS, $post_fields );
curl_setopt ( $this->curl, CURLOPT_RETURNTRANSFER, true );
curl_setopt ( $this->curl, CURLOPT_CONNECTTIMEOUT,20);
curl_setopt ( $this->curl, CURLOPT_TIMEOUT,10);
curl_setopt ( $this->curl, CURLOPT_HEADER, true );
curl_setopt ( $this->curl, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt ( $this->curl, CURLOPT_COOKIE, $this->cookie_name );
curl_setopt ( $this->curl, CURLOPT_COOKIEJAR, $this->cookie_name );
curl_setopt ( $this->curl, CURLOPT_COOKIEFILE, $this->cookie_name );
curl_setopt ( $this->curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1');
$result = curl_exec ( $this->curl );

this is how it should be:
Code:
Bölüm resimleri, dizi indirme ve altyazı linkine aşağıdan ulaşabilirsiniz.

this is how it is posted:
Code:
Bölüm resimleri, dizi indirme ve altyazı linkine aşağıdan ulaşabilirsiniz.

Thanks
 
14 comments
ok it seems you need to get the content type to post as UTF-8.

try:
Code:
curl_setopt($this->curl, CURLOPT_HTTPHEADER , array(
     'Content-Type: application/x-www-form-urlencoded; charset=utf-8',
));

*after* setting CURLOPT_POST and CURLOPT_POSTFIELDS.
 
i already do that, but if am using the httpheader i get that error, if i don't use it the script is posting.

PHP:
$url_vars = $this->array_to_http(array(
            'do'    => 'newthread',
            'f'        => $forum_id,
        ));
                
$this->curl = curl_init();
curl_setopt($this->curl,CURLOPT_URL, $this->phpbb_url . 'newthread.php?' . $url_vars );
curl_setopt($this->curl,CURLOPT_RETURNTRANSFER, true);
curl_setopt($this->curl,CURLOPT_CONNECTTIMEOUT,"10");
curl_setopt($this->curl,CURLOPT_TIMEOUT,"10");
curl_setopt($this->curl, CURLOPT_HEADER, false );
curl_setopt($this->curl,CURLOPT_COOKIEFILE,$this->cookie_name );
$p= curl_exec($this->curl);
$error=curl_error($this->curl);
curl_close($this->curl);

preg_match('%name="securitytoken" value="(.*?)" />%',$p,$security123);
// Generate post string
        $post_fields = array(
            'sbutton'                => 'Submit New Thread',
            'do'                => 'postthread',
            'message'            => $message,
            'f'                    => $forum_id,
            'securitytoken' => $security123[1],
            'subject'            => $topic_title,
            'loggedinuser'      => '1',
            'wysiwyg'            => 0,
            'prefixid'            => '',
            
        );
        $url_vars = array(
            'do'    => 'postthread',
            'f'        => $forum_id,
        );
        //printr($post_fields);
        // Init curl
        $this->curl = curl_init();
        // Set options
        curl_setopt ( $this->curl, CURLOPT_URL, $this->phpbb_url . 'newthread.php?' . $url_vars );
        curl_setopt ( $this->curl, CURLOPT_POST, true );
        curl_setopt ( $this->curl, CURLOPT_POSTFIELDS, $post_fields );
        curl_setopt ( $this->curl, CURLOPT_RETURNTRANSFER, true );
        curl_setopt ( $this->curl, CURLOPT_CONNECTTIMEOUT,20);
        curl_setopt ( $this->curl, CURLOPT_TIMEOUT,10);
        curl_setopt ( $this->curl, CURLOPT_HEADER, true );
        curl_setopt ( $this->curl, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt ( $this->curl, CURLOPT_COOKIE, $this->cookie_name );
        curl_setopt ( $this->curl, CURLOPT_COOKIEJAR, $this->cookie_name );
        curl_setopt ( $this->curl, CURLOPT_COOKIEFILE, $this->cookie_name );
        curl_setopt ( $this->curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1');
 
the same :(
Bölüm resimleri, dizi indirme ve altyazı linkine aÃ…Ÿağıdan ulaÃ…Ÿabilirsiniz.
 
The text is ok now :)
The only problem is that i get a notice
Code:
Notice: iconv() [function.iconv]: Detected an illegal character in input string in

but is working

i read the rext from a txt file which i save it like this
Code:
$fh = fopen($file,'wb');
fwrite($fh,"\xEF\xBB\xBF");
fwrite($fh,$ppost);
fclose($fh);
 
you can ignore that error with @ ( some text's encoding is not being recognized by iconv)
PHP:
$message = @iconv("UTF-8","Windows-1252//IGNORE",$message);
 
@ t3dor
Are you sure, you know how Unicode and encoding works?
It's pretty simple - If you have some data, Decode it to Unicode, work on it, and then encode it to utf-8 when saving to file.

BTW here's your culprit:
vBulletin's main character encoding is ISO-8859-1, not UTF-8

So you should instead encode to ISO-8859

But there's another catch
It is very common to mislabel Windows-1252 text with the charset label ISO-8859-1. A common result was that all the quotes and apostrophes (produced by "smart quotes" in Microsoft software) were replaced with question marks or boxes on non-Windows operating systems, making text difficult to read. Most modern web browsers and e-mail clients treat the MIME charset ISO-8859-1 as Windows-1252 in order to accommodate such mislabeling. This is now standard behavior in the draft HTML 5 specification, which requires that documents advertised as ISO-8859-1 actually be parsed with the Windows-1252 encoding

That's life, deal with it.
 
Status
Not open for further replies.
Back
Top