-2

This is my code:

<?php
$url = 'https://www.instagram.com/p/BachWpLgFAp/';
$content = file_get_contents($url);
$first_step = explode( 'edge_media_to_caption": {"edges": [{"node": {"text": "' , $content );
$second_step = explode("}}]}" , $first_step[1] );

$str = $second_step[0];
$str2 = substr($str, 0, -1);
print_r ($str2);

$caption = $str2;
if($user_message == "/test"){

            var_dump(bot('sendMessage',[
        'chat_id'=>$chat_id,
        'text'=>$caption,

    ]));
}

?>

Output data of $caption is: You\u2019re never too old to play in the mud. #teampixel photographer @samarthv.pattar throws a \u270c\ufe0f sign at his reflection after the rain.

How i can convert \u codes to utf-8?

Gajanan Kulkarni
  • 697
  • 6
  • 22
Mhdi
  • 11
  • 5
  • 1
    As far as I can see, all of this code is irrelevant to the question, which could actually be worded as "I have a string like `...throws a \u270c\ufe0f sign at his reflection...` and need to convert the `\u` escapes into UTF-8." – IMSoP Oct 20 '17 at 08:27
  • use [mb_convert_encoding()](http://php.net/manual/en/function.mb-convert-encoding.php) – Jeff Oct 20 '17 at 08:29
  • 1
    are you splitting a json-string manually? Why don't you just `json_decode` it?? – Jeff Oct 20 '17 at 08:30
  • @Gajanan Kulkarni . My code gives caption from Instagram posts, But come character in output is unicode and i want be utf-8 – Mhdi Oct 20 '17 at 08:30
  • @Jeff Where use? When i use json_decode($str2); there isn't output. – Mhdi Oct 20 '17 at 08:31
  • use it where you wanna convert it. `$caption=mb_convert_encoding($str2,'UTF-8');` – Jeff Oct 20 '17 at 08:31
  • @Jeff I try mb_convert_encoding but output data have not any changes. :( – Mhdi Oct 20 '17 at 08:33
  • @TomUdding Thanks, It works ;) – Mhdi Oct 20 '17 at 08:55

1 Answers1

2

This seems a little but dumb to me. You shouldn't try to parse the json string on your own. Why not just extract the whole json string and then use json_decode.

$url = 'https://www.instagram.com/p/BachWpLgFAp/';
$content = file_get_contents($url);
if (preg_match('/<script[^>]+>\s*window\._sharedData[^\{]+(\{.*?);\s*<\/script>/ms', $content, $m)) {
    $json = $m[1];
    $jsonData = json_decode($json, true);
}

After getting the jsonData, you could such search in the given array for edge_media_to_caption.

foreach ($jsonData['entry_data']['PostPage'] as $page) {
    $graphql = $page['graphql'];
    var_dump($graphql['shortcode_media']['edge_media_to_caption']);
}

Btw. there's an official Instagram Api, which should be the official way to get your desired information. https://www.instagram.com/developer/endpoints/media/ What you are currently doing might be against their terms of usage.

Philipp
  • 15,377
  • 4
  • 35
  • 52