dsgtew3241 2016-05-30 16:36
浏览 130
已采纳

在PHP中使用curl获取robots.txt

I am trying to execute a robots.txt HTTP request. For that I am using PHP and curl. This is the code I tried:

<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "robots.txt");
curl_setopt($ch, CURLOPT_HTTPGET, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Host: www.stackoverflow.com'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
echo $result;

Nothing gets printed to the screen. Any idea what am I missing?

  • 写回答

1条回答 默认 最新

  • dongshu4221 2016-05-30 16:40
    关注

    In CURLOPT_URL you must write full host and path

    For example: http://www.stackoverflow.com/robots.txt

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?