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

时间:2015-12-22 02:36:22

标签: 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($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);      
    ?>

1 个答案:

答案 0 :(得分:1)

链接https://iiitd.ac.in/正在重定向到https://www.iiitd.ac.in/,因此您需要修改您的卷曲代码。您需要将CURLOPT_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);