3

I'am trying to get the read and like number of an page.

enter image description here

The url is: https://mp.weixin.qq.com/s/NPavBeHc8VdWXeSL6kfLRg (you must activate a mobile user agent to see read and like number. There are at the bottom left of the page. You must refresh the page.)

The problem is that read and like number are hidden when isn't a mobile user agent that visit the site.

So i tried to use file_get_contents() with a context that send user agent with http as other stackoverflow post said. But didn't work :(.

There is my code:

$url = 'https://mp.weixin.qq.com/s/NPavBeHc8VdWXeSL6kfLRg';


$opts = array('http' =>
    array(
        'header'  => 'User-agent: Mozilla/5.0 (Linux; Android 4.4.4; HM NOTE 1LTEW Build/KTU84P) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/33.0.0.0 Mobile Safari/537.36 MicroMessenger/6.0.0.54_r849063.501 NetType/WIFI',
    )
);

$context  = stream_context_create($opts);

$result = file_get_contents($url, false, $context);

print_r($result);

I also tried to modify the php.ini with

user_agent= "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Mobile Safari/537.36"

Jilu
  • 197
  • 1
  • 14

1 Answers1

1

From HTTP context options:

user_agent string

Value to send with User-Agent: header. This value will only be used if user-agent is not specified in the header context option above.

If you opt for header, you need to set the header name as well, not just the value!

Álvaro González
  • 142,137
  • 41
  • 261
  • 360
  • 1
    some thing like this ? $opts = array('http' => array( 'header' => 'User-agent: Mozilla/5.0 (Linux; Android 4.4.4; HM NOTE 1LTEW Build/KTU84P) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/33.0.0.0 Mobile Safari/537.36 MicroMessenger/6.0.0.54_r849063.501 NetType/WIFI', ) ); – Jilu May 18 '17 at 14:18