我一直在尝试在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的内容。
答案 0 :(得分: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;
?>
答案 1 :(得分:0)
有一个输入卫生问题,$ address变量的值中有一个空格。