PHP + cURL无法正常工作

yol*_*olo 0 php curl

尝试使用cURL和PHP获取某个URL的内容.该代码应该在sourceforge.net项目Web托管服务器上运行.

码:

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

?>
Run Code Online (Sandbox Code Playgroud)

我已经检查过cURL是否受支持.但上面的代码不起作用,页面加载到超时没有输出.我也尝试过编码的url.为什么?

错误503 Service Unavailable.PHP版本5.3.2

den*_*nil 5

您可能想要使用file_get_contents

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

您还可以在file_get_contents ex中设置文件:

$content = file_get_contents('textfile.txt');
Run Code Online (Sandbox Code Playgroud)

有关函数file_get_conents的更多信息

使用cUrl时我注意到的一些信息:

我在使用cUrl时也注意到的一件事是,当URL有http或https时,它的工作方式不同.您需要确保代码可以处理此问题