Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

file_get_contents() for short urls

file_get_contents() doesn't read data for short urls Example:

  • http://wp.me/pbZy8-1WM,
  • http://bit.ly/d00E2C

Please help me in handle this. OR Is there any CURL function to handle above links?

like image 965
praneeth Avatar asked May 30 '11 05:05

praneeth


People also ask

What is the use of file_get_contents () function?

The file_get_contents() reads a file into a string. This function is the preferred way to read the contents of a file into a string. It will use memory mapping techniques, if this is supported by the server, to enhance performance.

Which is faster cURL or file_get_contents?

cURL is capable of much more than file_get_contents . That should be enough. FWIW there's little difference with regards to speed.

What is the difference between file_get_contents () function and file () function?

file — Reads entire file contents into an array of lines. file_get_contents — Reads entire file contents into a string.

What does file_ get_ contents return?

This function is similar to file(), except that file_get_contents() returns the file in a string, starting at the specified offset up to length bytes. On failure, file_get_contents() will return false . file_get_contents() is the preferred way to read the contents of a file into a string.


2 Answers

This in general works fine. If you find it doesn't do the right thing you can explicitly use a stream context:

$url = "http://bit.ly/d00E2C";
$context = stream_context_create(array('http' => array('max_redirects' => 5)));
$val = file_get_contents($url, false, $context);

should do it. No need to touch CURL for that.

like image 117
Femi Avatar answered Sep 25 '22 00:09

Femi


On my machine, I cannot replicate your problem; I receive the page as intended. However, should the issue be with the redirect, this may solve your problem.

<?php
$opts = array(
    'http' => array(
        'follow_location' => 1,
        'max_redirects' => 20
    )
);
$context = stream_context_create($opts);
echo file_get_contents('http://wp.me/pbZy8-1WM', false, $context);

I imagine there may be a directive that toggles redirect following, but I have not yet found it. I will edit my answer should I.

like image 25
erisco Avatar answered Sep 25 '22 00:09

erisco