【问题标题】:CURL POST/GET URL doesn't work when stored in variableCURL POST/GET URL 存储在变量中时不起作用
【发布时间】:2013-01-20 15:32:19
【问题描述】:

简而言之……

不起作用

$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;

作品

$ch = curl_init(); 
    curl_setopt ($ch, CURLOPT_URL, "http://www.example.com/test/index.php?id=1&token=723648723"); 
    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;

什么给了?我试过 urlencode、urldecode、rawurlencode 都没有成功。

显然,在浏览器中发布网址可以正常工作。

编辑:我可能应该添加 url 是从另一个 curl 获得的,就在这个之前。如果我将 url 存储在一个变量中它可以工作,但如果我让另一个 curl 设置变量,它不会。

【问题讨论】:

  • 你比较var_dump($url,'http://www.example.com/test/index.php?id=1&amp;token=723648723');了吗?
  • 您可能正在做某事。它们看起来相同,但字符串计数发生了变化。一个是字符串(123),另一个是字符串(115)。
  • var_dump(trim($url),'http://www.example.com/test/index.php?id=1&amp;token=723648723'); 怎么样?
  • 返回相同。很奇怪。
  • 123个字符的字符串中包含“&”而另一个“&”

标签: php curl


【解决方案1】:

试试这个:

$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;

更新 3:清理 html、空格、新行、替换 &amp;amp; 并强制字符串数据类型...

$url = (string) trim(strip_tags($url));
$url = str_replace('&amp;', '&', $url);
$ch  = curl_init($url);
// etc

【讨论】:

  • 它也不起作用。返回“0”。这真的很奇怪,因为当我手动设置 $url 变量时,它可以工作。但是如果我让我之前的 curl 设置 $url,它就不起作用了。当我在 curl 设置后回显 $url 时,它显示得很好。
  • @bastien @curl_close($ch); // close properly 也关闭之前的 curl 进程
  • @bastien 更新:强制字符串数据类型
  • 正确关闭所有内容仍然返回 0
  • 尝试强制字符串数据。还是不行.. 返回 0 =/
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多