file_get_contents不检索页面内容

时间:2016-08-18 05:33:00

标签: php file-get-contents

好的,在说这是重复之前只需阅读一下...... 我一直在尝试回显现在已禁用allow_url_fopen的网址的内容,我已尝试在堆栈溢出时发布的每个解决方案。实施例:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
$result = curl_exec($ch);
curl_close($ch); 

不工作

function curl_get_contents($url)
{
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, $url);

    $data = curl_exec($ch);
    curl_close($ch);

    return $data;
}

不工作

$url = "http://www.google.com";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
curl_close($ch);
echo $data;

不工作

fopen("cookies.txt", "w");
$url="http://adfoc.us/1575051";
$ch = curl_init();

$header=array('GET /1575051 HTTP/1.1',
    'Host: adfoc.us',
    'Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
    'Accept-Language:en-US,en;q=0.8',
    'Cache-Control:max-age=0',
    'Connection:keep-alive',
    'Host:adfoc.us',
    'User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36',
    );

    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
    curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,0);
    curl_setopt( $ch, CURLOPT_COOKIESESSION, true );

    curl_setopt($ch,CURLOPT_COOKIEFILE,'cookies.txt');
    curl_setopt($ch,CURLOPT_COOKIEJAR,'cookies.txt');
    curl_setopt($ch,CURLOPT_HTTPHEADER,$header);
    $result=curl_exec($ch);

    curl_close($ch);

不工作

// create the Gateway object
$gateway = new Gateway();

// set our url
$gateway->init($url);

// get the raw response, ignore errors
$response = $gateway->exec();

不工作

$file = "http://www.example.com/my_page.php";
if (function_exists('curl_version'))
{
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $file);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $content = curl_exec($curl);
    curl_close($curl);
}
else if (file_get_contents(__FILE__) && ini_get('allow_url_fopen'))
{
    $content = file_get_contents($file);
}
else
{
    echo 'You have neither cUrl installed nor allow_url_fopen activated. Please setup one of those!';
}

这不起作用。

我尝试使用的file_get_contents页面不在我的网站上。我正在尝试使用file_get_contents,因此我可以通过阅读页面并检查页面上是否存在某个单词来为网站所有者创建一个简单的API。

但是,如果有人有任何建议请在下面发布:)

1 个答案:

答案 0 :(得分:0)

您可以检查网站是否可用的第一个天气,例如示例代码

代码取自here

<?php
    $cURL = curl_init('http://www.technofusions.com/');
    curl_setopt ( $cURL , CURLOPT_RETURNTRANSFER , true );
    // Follow any kind of redirection that are in the URL
    curl_setopt ( $cURL , CURLOPT_FOLLOWLOCATION , true );
    $result  =  curl_exec ( $cURL );
    // Getting  HTTP response code
    $answer  =  curl_getinfo ( $cURL , CURLINFO_HTTP_CODE );
    curl_close ( $cURL );
    if ( $answer  ==  ' 404 ' ) {
      echo  ' The site not found (ERROR 404)! ' ;
    } else {
      echo  ' It looks like  everything is working fine ... ' ;
    }
  ?>

要获得完整的答案,您可以参加本教程Curl IN PHP