我正在尝试调用远程站点上页面中的网址。决定使用curl。在远程站点上,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 对象已经登录到一个表单,设置了 cookies,在另一个页面上发布了一个表单,现在这是我必须关注的最后一个链接。
这是我正在使用的 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'] 复制并粘贴到浏览器中,它就可以工作。完全迷失了,时间浪费了几个小时,任何帮助将不胜感激;)
最佳答案
尝试按如下方式修改您的代码:
$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 有一个内置函数可以执行相同的操作。
关于php curl 使用 GET 发送变量发送奇怪的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10230774/