0

我一直在尝试在 PHP 中使用带有动态地址的 cURL(即通过 POST 变量设置),但它不起作用!

<?php
$address = $_POST['address'];

//echo $address;
//$address = "http://twitter.com";

$curl_handle=curl_init();
curl_setopt($curl_handle,CURLOPT_URL, $address);
curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,2);
curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1);
$buffer = curl_exec($curl_handle);
curl_close($curl_handle);

if (empty($buffer)) {
    echo "Sorry, ".$address." are a bunch of poopy-heads.<p>";
}
else {
    echo $buffer;
}
?>

我知道 POST 正在工作,因为我可以回显 $address 变量并查看我发送的 $address 的内容。

4

2 回答 2

0

好的,既然你知道$_POST它正在工作,那么让我们看看你的 cURL。

这是我通常使用的功能。也许你可以试一试?

代码 :

function getCurl(
                $url,
                $returntransfer=1, $httpproxytunnel=1, $referer=null,
                $useragent=null, $header=null, $httpheader=null,
                $proxy=null, $port=null
                )
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, $returntransfer);
    curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, $httpproxytunnel);

    if ($referer!=null)
        curl_setopt($ch, CURLOPT_REFERER, $referer);
    if ($useragent!=null)
        curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
    if ($header!=null)
        curl_setopt($ch, CURLOPT_HEADER, $header);
    if ($httpheader!=null)
        curl_setopt($ch, CURLOPT_HTTPHEADER, $httpheader);
    if ($proxy!=null)
        curl_setopt($ch, CURLOPT_PROXY, $proxy);
    if ($port!=null)
        curl_setopt($ch, CURLOPT_PORT, $port);

    curl_setopt($ch, CURLOPT_TIMEOUT, 40);

    $result['DATA'] = curl_exec($ch);
    $result['INFO'] = curl_getinfo($ch);
    $result['ERROR'] = curl_error($ch); 

    return $result;
}

function getFile($url)
{
    $data = getCurl($url);

    return $data['DATA'];
}

并像这样使用它:

<?php
     $fileContents = getFile("http://www.somedomain.com/a-path-to-your-file.html");
     echo $fileContents;
?>
于 2012-04-07T14:10:54.653 回答
0

存在输入卫生问题,其中 $address 变量的值中有一个空格。

于 2013-09-28T22:39:48.697 回答