[英]php curl sending vars using GET wierd results
我正在嘗試在遠程站點的頁面中調用URL。 決定使用curl。 在遠程站點上,URL變量顯示為:
$_REQUEST Array
(
[var1] => val1
[amp;var2] => val2
[amp;var3] => val3
)
被調用的URL是:
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']復制並粘貼到瀏覽器中,它將起作用。 完全失去了,浪費了數小時,任何幫助將不勝感激;)
嘗試如下修改您的代碼:
$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具有一個內置功能,可完成相同的功能。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.