Explorer
Explorer

Reputation: 177

Getting Unknown Charcters from Twitter Api

I am using https://api.twitter.com/1.1/statuses/user_timeline.json twitter api for getting tweets from some particular account and getting all tweets properly except tweets in other language rather than English.

Php Code for making request to Twitter is below:

    $ch = curl_init();
         curl_setopt($ch,CURLOPT_POST,1);
         curl_setopt($ch,CURLOPT_POSTFIELDS,"grant_type=client_credentials");        
         curl_setopt($ch,CURLOPT_URL,$url);
         curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
         curl_setopt($ch,CURLOPT_CONNECTTIMEOUT, 5);
         curl_setopt($ch,CURLOPT_USERAGENT, "Tweet Fetcher PHP 0.0.1");
         curl_setopt($ch,CURLOPT_FOLLOWLOCATION, 1);
         curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);
         curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,2);
         curl_setopt($ch,CURLOPT_HTTPHEADER,array('Authorization: '.$authType.'
 '.$authValue,'Content-Type: application/x-www-form-urlencoded;charset=UTF-8'));
         $result = curl_exec($ch);
         curl_close($ch); 

Getting some tweets Results as

मेहनत और मोबाइल ने चमकाई

How to get this string in proper charset?

Update Code:

$tweet = json_decode($result);

But getting same results

Upvotes: 0

Views: 163

Answers (1)

Terence Eden
Terence Eden

Reputation: 14334

Let's take a look at the API response you're getting.

https://apigee.com/snapshot/twitter?snapId=apigee-console-snapshots-1388552400000_03b4cb5a-7817-4eba-8e66-1d622deb8c27

{
  "created_at": "Wed Jan 08 10:02:49 +0000 2014",
  "id": 420858117375553540,
  "id_str": "420858117375553537",
  "text": "पंजाब ड्रग्स गिरोह में मंत्री बिक्रम मजीथीआ का नाम सामने आया। श्री राहुल गांधी ने इस बारे में बहुत पहले बताया था। http://t.co/TQHGZxALQU",
  "source": "web",
  "truncated": false,
  "in_reply_to_status_id": null,
 ...etc...

There is literally nothing wrong there. The "text" is UTF-8 and will be handled properly by PHP.

What is probably happening is that you haven't set the meta tags on your page to display UTF-8. It's probably set to a Windows Charset.

At the top of your page, use something like

header('Content-Type: text/html; charset=utf-8');

To force the output to the correct encoding.

Upvotes: 0

Related Questions