【问题标题】:php curl sending vars using GET wierd resultsphp curl 使用 GET 奇怪的结果发送 vars
【发布时间】:2012-04-19 14:53:36
【问题描述】:

我正在尝试调用远程站点页面中的 url。决定使用 curl。在远程站点上,url 变量显示为:

$_REQUEST   Array
(
    [var1] => val1
    [amp;var2] => val2
    [amp;var3] => val3
)

被调用的网址是:

http://site.com/controller/action.php?var1=val1&var2=val2&var3=val3

请注意,我没有在 url 中使用 &,但请求全局有它,或者几乎 - 它有 amp; 而不是 & 而不是 &,就像我使用的那样!!!并不是说它应该有任何一个。

curl 对象已经登录到一个表单,设置了 cookie,在另一个页面上发布了一个表单,现在这是我必须关注的最后一个链接。

这是我正在使用的 php:

    curl_setopt($this->ch, CURLOPT_URL, "http://site.com/controller/action.php?var1=val1&var2=val2&var3=val3");
    curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($this->ch, CURLOPT_REFERER, "http://site.com/");
    curl_setopt($this->ch, CURLOPT_VERBOSE, 1);
    curl_setopt($this->ch, CURLOPT_COOKIEJAR, $this->cookie);
    curl_setopt($this->ch, CURLOPT_COOKIEFILE, $this->cookie);
    curl_setopt($this->ch, CURLOPT_POST, 0);
    curl_setopt($this->ch, CURLOPT_HTTPGET, 1);
    $output = curl_exec($this->ch);
    $info = curl_getinfo($this->ch);

在此之后我运行了另一个 curl 请求,但我仍然登录,所以问题不是 cookie。因为使用 $_POST 的最后一个请求(登录表单)已将 CURLOPT_POST 设置为 0,将 CURLOPT_HTTPGET 设置为 1。 这是 $info 的输出:

info    Array
(
    [url] => http://site.com/controller/action.php?var1=val1&var2=val2&var3=val3
    [content_type] => text/html; charset=utf-8
    [http_code] => 403
    [header_size] => 403
    [request_size] => 541
    [filetime] => -1
    [ssl_verify_result] => 0
    [redirect_count] => 0
    [total_time] => 0.781186
    [namelookup_time] => 3.7E-5
    [connect_time] => 3.7E-5
    [pretransfer_time] => 4.2E-5
    [size_upload] => 1093
    [size_download] => 3264
    [speed_download] => 4178
    [speed_upload] => 1399
    [download_content_length] => 3264
    [upload_content_length] => 0
    [starttransfer_time] => 0.781078
    [redirect_time] => 0
    [certinfo] => Array
        (
        )

)

如果我将 $info['url'] 复制并粘贴到浏览器中,它就可以工作。完全丢失了,时钟丢失了几个小时,任何帮助将不胜感激;)

【问题讨论】:

  • .... http 403 发生在我的代码中,因为在 $_GET[] 或 $_REQUEST[] 中找不到 $var2

标签: php curl get


【解决方案1】:

尝试如下修改您的代码:

$data = array('val1'=>"val1", 'val2'=>"val2", 'val3'=>"val3");
curl_setopt($this->ch, CURLOPT_URL, "http://site.com/controller/action.php");
curl_setopt($this->ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($this->ch, CURLOPT_REFERER, "http://site.com/");
curl_setopt($this->ch, CURLOPT_VERBOSE, 1);
curl_setopt($this->ch, CURLOPT_COOKIEJAR, $this->cookie);
curl_setopt($this->ch, CURLOPT_COOKIEFILE, $this->cookie);
curl_setopt($this->ch, CURLOPT_POST, 0);
curl_setopt($this->ch, CURLOPT_HTTPGET, 1);
$output = curl_exec($this->ch);
$info = curl_getinfo($this->ch);

编辑

根据 Brad 的评论删除了自定义编码函数 - 你不需要它,因为 PHP 有一个内置函数可以做同样的事情。

【讨论】:

  • http_build_query() 工作正常时,你为什么还要编写自己的编码函数?
  • 刚刚从我正在处理的当前代码库中获取了这个函数——实际上不知道为什么最初的开发者会这样写。我更新了我的答案以反映这一点。
  • 这似乎是一个帖子,但操作人员想要一个 get.. 想法?
  • 不,将 CURLOPT_POST 设置为 0 可以解决这个问题。我认为 OP 面临的问题是 URL 编码 - 他传递的是正在编码的字符,而不是按字面意思处理 URL。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多