PHP Curl在某些情况下工作,而在某些情况下不工作

PHP Curl在某些情况下工作,而在某些情况下不工作,php,Php,此代码运行良好 <?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(

此代码运行良好

  <?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);      
    ?>

但下面两组代码不起作用。我没有收到错误,但在这些情况下,浏览器加载栏只是不断旋转,从不停止。页面显示加载和加载很长时间,但没有从URL加载任何内容。问题在哪里

 <?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);      
    ?>

链接
https://iiitd.ac.in/
正在重定向到
https://www.iiitd.ac.in/
因此您需要修改curl代码。您需要将
CURLOPT\u FOLLOWLOCATION
设置为
true

请查看以下解决方案:

<?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);

我的问题是;在第二个示例中,为什么要运行两次curl?