php curl使用GET奇怪的结果发送变种

时间:2012-04-19 14:53:36

标签: php curl get

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

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

被调用的网址是:

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

请注意,我没有在网址中使用&,但请求全局包含它,或者差不多 - 它有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']复制到浏览器中就可以了。完全丢失,时间丢失了几小时,任何帮助将不胜感激;)

1 个答案:

答案 0 :(得分:5)

尝试按如下方式修改代码:

$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有​​一个内置函数可以做同样的事情。