Php CURL POST/GET URL不';存储在变量中时不工作
简言之 不起作用Php CURL POST/GET URL不';存储在变量中时不工作,php,curl,Php,Curl,简言之 不起作用 $url = "http://www.example.com/test/index.php?id=1&token=723648723"; <-- Set by previous Curl $ch = curl_init(); curl_setopt ($ch, CURLOPT_URL, $url); curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Window
$url = "http://www.example.com/test/index.php?id=1&token=723648723"; <-- Set by previous Curl
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6");
curl_setopt ($ch, CURLOPT_TIMEOUT, 60);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt ($ch, CURLOPT_COOKIEFILE, $cookie);
$html = curl_exec ($ch);
echo $html;
有什么好处?我尝试过URLENCDE、URLEDECODE、RAWURLENCDE,但没有成功
显然,在浏览器中发布url效果很好
EDIT:我可能应该添加url,该url是从运行在此之前的另一个curl中获得的。如果我将url存储在一个变量中,它可以工作,但如果我让另一个curl设置变量,它就不能工作。尝试以下方法:
$url = "http://www.example.com/test/index.php?id=1&token=723648723"; // < -- Set by previous Curl
$ch = curl_init($url); // init with the given $url here
// remove curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6");
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
$html = curl_exec($ch);
if ($html === false) // check for errors
{
// throw new Exception('Curl error: ' . @curl_error($ch));
echo 'Curl error: ' . @curl_error($ch);
}
@curl_close($ch); // close properly
echo $html;
您是否比较了
var\u dump($url,'http://www.example.com/test/index.php?id=1&token=723648723');代码>?你可能发现了什么。它们看起来相同,但字符串计数发生变化。一个是字符串(123),另一个是字符串(115)。那么var\u dump(trim($url)呢http://www.example.com/test/index.php?id=1&token=723648723');代码>?它返回相同的值。奇怪。包含123个字符的字符串包含“&;”,而另一个“&”也不起作用。返回“0”。这真的很奇怪,因为当我手动设置$url变量时,它就工作了。但是如果我让我以前的curl设置$url,它就不起作用了。当我在curl设置$url后回显它时,它显示得很好。@bastien@curl\u close($ch);//正确关闭
关闭上一个卷曲过程too@bastien更新:强制字符串数据类型正确关闭所有内容仍然返回0强制字符串数据。还是不行。。返回0=/
$url = "http://www.example.com/test/index.php?id=1&token=723648723"; // < -- Set by previous Curl
$ch = curl_init($url); // init with the given $url here
// remove curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6");
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
$html = curl_exec($ch);
if ($html === false) // check for errors
{
// throw new Exception('Curl error: ' . @curl_error($ch));
echo 'Curl error: ' . @curl_error($ch);
}
@curl_close($ch); // close properly
echo $html;
$url = (string) trim(strip_tags($url));
$url = str_replace('&', '&', $url);
$ch = curl_init($url);
// etc