2012-04-19 85 views
0

我正在嘗試在遠程站點上的頁面中調用一個url。決定使用捲曲。在遠程站點的URL瓦爾都呈現爲:php curl使用GET wierd結果發送變量

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

我已經在這之後跑到另一個捲曲的要求,我仍然登錄所以問題不是餅乾。因爲最後一個請求(登錄表單)使用$ _ POST已設置了CURLOPT_POST爲0,CURLOPT_HTTPGET爲1 這裏是$信息輸出:

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 
     (
     ) 

) 

如果我複製和過去$信息[」 url']到它的瀏覽器中。完全失去了,失去了對時鐘時間,任何幫助,將不勝感激;)

+0

.... http 403發生在我的代碼中,因爲在$ _GET []或$ _REQUEST [] – 2012-04-19 14:58:13

回答

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

編輯按照布拉德的評論

刪除自定義編碼功能 - 你不需要它,因爲PHP有​​一個內置函數可以做同樣的事情。

+0

中找不到$ var2爲什麼當'http_build_query()'工作時編寫自己的編碼函數正好? – Brad 2012-04-19 15:07:52

+0

剛剛從我正在編寫的當前代碼庫中抓取這個函數 - 實際上不知道原始開發人員爲什麼這樣寫這個函數。我更新了我的答案以反映這一點。 – Elie 2012-04-19 15:10:27

+0

這似乎是一個職位,但操作想要得到..思想? – ladieu 2013-03-03 20:03:57