3

I'm trying the following code as I can't use php file_get_contents due to redirecting, but this is returning nothing:

function file_get_contents_curl($url) {
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

    $data = curl_exec($ch);

    curl_close($ch);

    return $data;
}


$url = 'http://www1.macys.com/shop/product/michael-michael-kors-wallet-jet-set-monogram-ziparound-continental?ID=597522&CategoryID=26846&RVI=Splash_5';
$html = file_get_contents_curl($url);
echo "html is ".$html;
StudioTime
  • 22,603
  • 38
  • 120
  • 207

3 Answers3

9

Quickly looking at the output of:

curl --max-redirs 3 -L -v $url shows that 302 redirection redirects to same url .. while setting the cookies.

setting cookie jar seems to 'fix' it:

curl -c /tmp/cookie-jar.txt --max-redirs 3 -L -i $url

So .. enable cookie (jar)

Edit:

You can add a line to your existing code to make it work:

curl_setopt($ch, CURLOPT_COOKIEJAR, dirname(__FILE__).DIRECTORY_SEPERATOR.'c00kie.txt');
Prasanth
  • 5,230
  • 2
  • 29
  • 61
Teftin
  • 846
  • 7
  • 7
  • 1
    Thanks so much, added the following and all ok now (gave cookie.txt 777 perms) $cookie_file = "/home/***/cookiejar_keep/cookie.txt"; curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file); curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file); – StudioTime Sep 13 '12 at 07:02
2

You can use CURLOPT_VERBOSE option to debug your requests.

Writes output to STDERR, or the file specified using CURLOPT_STDERR.

$curl_debug_file = __DIR__.DIRECTORY_SEPARATOR.'curl.log';
$fh = fopen($curl_debug_file, "w+");
curl_setopt($handle, CURLOPT_STDERR, $fh);
curl_setopt($ch, CURLOPT_VERBOSE, true);
Alexander Larikov
  • 2,328
  • 15
  • 15
  • Looks like you have a typo, it should be curl_setopt($ch, CURLOPT_STDERR, $fh); right? – Mike Nov 19 '15 at 14:23
0

If you run

echo curl_error($ch);

right after curl_exec, you'll see this:

Maximum (20) redirects followed

Find out why it's redirecting and where

vvondra
  • 3,022
  • 1
  • 21
  • 34
  • I ran the same thing. I got no error. Although running `print_r(curl_getinfo($ch));` shows a single redirect to the same URL. – Danny Beckett Sep 13 '12 at 06:48