Php 通过POST变量设置cURL地址
我一直试图在PHP中使用带有动态地址(即通过POST变量设置)的cURL,但它不起作用Php 通过POST变量设置cURL地址,php,ajax,curl,Php,Ajax,Curl,我一直试图在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_CONNE
<?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的内容。好的,既然您知道
$\u 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;
?>
并像这样使用它:
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变量的值中有一个空格。那么您有什么错误或问题?不工作不好请检查您的$\u POST['address']
该$\u POST['address']工作正常,请查看我问题的底线。没有错误,它不会返回我通过POST指定的任何地址的任何内容。因此,如果您不打算使用它们,为什么要添加所有额外选项?@lawrencerone,因为我有时可能需要…;-)隐马尔可夫模型。。。我尝试了$fileContents=getFile($address);echo$fileContents;然后在这种情况下,您应该使用return$data
这样您就可以访问errors&info数组;p@CharlesJohnThompsonIII既然你说你可以得到$\u POST['address']
,为什么不试试最简单的方法呢:echo file\u得到的内容($address)
返回什么?