2

This Code is Working Well

  <?php 
            // create curl resource 
            $ch = curl_init(); 

            // set url 
            curl_setopt($ch, CURLOPT_URL, "http://ipdev.in/"); 

            //return the transfer as a string 
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 

            // $output contains the output string 
            $output = curl_exec($ch); 
            echo $output;
            // close curl resource to free up system resources 
            curl_close($ch);      
    ?>

But below two bunch of codes are not working. I am not getting an error but in these cases browser loading bar is just revolving and revolving and never stops. The page shows loading and loading for a long time but nothing loads from URL. Where is the problem ?

 <?php 
            // create curl resource 
            $ch = curl_init(); 

            // set url 
            curl_setopt($ch, CURLOPT_URL, "https://iiitd.ac.in/"); 

            //return the transfer as a string 
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 

            // $output contains the output string 
            $output = curl_exec($ch); 
            echo $output;
            // close curl resource to free up system resources 
            curl_close($ch);      
    ?>

    <?php 
            // create curl resource 
            $ch = curl_init(); 

            // set url 
            curl_setopt($ch, CURLOPT_URL, "http://iiitd.ac.in"); 

            //return the transfer as a string 
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 

            // $output contains the output string 
            $output = curl_exec($ch); 
    echo $output;
            // close curl resource to free up system resources 
            curl_close($ch);      
    ?>
1
  • My question is; Why are you running the curl twice in the second example? Commented Dec 22, 2015 at 2:51

1 Answer 1

1

the link https://iiitd.ac.in/ is redirecting to https://www.iiitd.ac.in/ so you need to modify your curl code. You need to set CURLOPT_FOLLOWLOCATION as true.

Have a look on below solution:

<?php
// create curl resource
$ch = curl_init();

// set url
curl_setopt($ch, CURLOPT_URL, "https://iiitd.ac.in/");

//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

// added follow location
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

// $output contains the output string
$output = curl_exec($ch);
echo $output;
// close curl resource to free up system resources
curl_close($ch);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.