通过 POST 变量设置 cURL 地址


Set cURL address via POST variable

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

<?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的内容。

好的,既然你知道$_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;
?>

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