简体   繁体   English

PHP + cURL不起作用

[英]PHP + cURL not working

Trying to get the content of the a certain URL using cURL and PHP. 尝试使用cURL和PHP获取某个URL的内容。 The code is supposed to run on the sourceforge.net project web hosting server. 该代码应在sourceforge.net项目Web托管服务器上运行。

code: 码:

<?php

function get_data($url)
{
  $ch = curl_init();
  $timeout = 10;
  curl_setopt($ch,CURLOPT_URL,$url);
  curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
  curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
  $data = curl_exec($ch);
  curl_close($ch);
  return $data;
}

$url1 = urlencode("http://www.google.com"); 
$url2 = "http://www.google.com";
$output = get_data($url2);
echo $output;

?>

I've checked that cURL is supported. 我检查了是否支持cURL。 But the above code is not working, the page is loading till timeout with no output. 但是上面的代码不起作用,页面正在加载直到超时,没有输出。 I've tried the encoded url as well. 我也尝试了编码的网址。 Why? 为什么?

Error 503 Service Unavailable . 错误503 Service Unavailable PHP version 5.3.2 PHP版本5.3.2

You might want to use file_get_contents 您可能要使用file_get_contents

$content = file_get_contents('http://www.google.com');
/some code here if needed/
return $content;

You can also set files inside file_get_contents ex: 您也可以在file_get_contents内设置文件,例如:

$content = file_get_contents('textfile.txt');

More information about the function file_get_conents 有关函数file_get_conents的更多信息

Some info which I noticed when working with cUrl: 我在使用cUrl时注意到的一些信息:

One thing I've also noticed when working with cUrl is that it works differently when the URL has http or https. 在使用cUrl时,我还注意到的一件事是,当URL具有http或https时,它的工作方式有所不同。 You need to make sure that you code can handle this 您需要确保您的代码可以处理此问题

I replaced my curl code with yours and its not working. 我将卷曲代码替换为您的卷曲代码,但无法正常工作。 I tried with 'gmail.com' and it showed fine with my code and with yours it gave a '301 Moved' Error. 我尝试使用“ gmail.com”,它在我的代码中显示正常,与您的代码一起显示“ 301移动”错误。

My Code is as follows: 我的代码如下:

function get_web_page($url)
{
        //echo "curl:url<pre>".$url."</pre><BR>";
    $options = array(
        CURLOPT_RETURNTRANSFER => true,     // return web page
        CURLOPT_HEADER         => false,    // don't return headers
        CURLOPT_FOLLOWLOCATION => true,     // follow redirects
        CURLOPT_ENCODING       => "",       // handle all encodings
        CURLOPT_USERAGENT      => "spider", // who am i
        CURLOPT_AUTOREFERER    => true,     // set referer on redirect
        CURLOPT_CONNECTTIMEOUT => 15,      // timeout on connect
        CURLOPT_TIMEOUT        => 15,      // timeout on response
        CURLOPT_MAXREDIRS      => 10,       // stop after 10 redirects

    );

    $ch      = curl_init($url);
    curl_setopt_array( $ch, $options );
    $content = curl_exec( $ch );
    $err     = curl_errno( $ch );
    $errmsg  = curl_error( $ch );
    $header  = curl_getinfo( $ch,CURLINFO_EFFECTIVE_URL );
    curl_close( $ch );

    $header['errno']   = $err;
    $header['errmsg']  = $errmsg;

    //change errmsg here to errno
    if ($errmsg)
    {
        echo "CURL:".$errmsg."<BR>";
    }
    return $content;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM