2014-01-10 37 views
0

請幫我我怎樣才能使這個工作,如果它是個右擊辦法做到這一點,如果它不是你會建議發佈參數用一個for循環創建一個字符串,然後使用捲曲後

$str = ''; 
for($i = 11; $i <= 20; $i++) 
{ 
$str .= $i . ' ';  
} 
$ch = curl_init(); //http post to another server 
curl_setopt($ch, CURLOPT_URL,"http://xxxx"); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS,"username=$username&password=$password&string=$str"); 

// receive server response ... 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
$server_output = curl_exec ($ch); 
print_r($server_output); 
curl_close ($ch); 
+0

問題是什麼?你得到什麼錯誤?你在期待什麼? – Kami

+0

也許空間是一個問題? – putvande

+0

在手動創建'CURL_POSTFIELDS'值之前,使用'curl_escape'對其進行編碼 - 空格將是一個問題,因爲@putvande建議。另外,關於您實際遇到的問題的提示將非常有用 – DaveyBoy

回答

0

如果你有機會向你捲曲調用腳本,嘗試添加:

var_dump($_POST); 

,並看打印的內容。

我只是讓你的代碼更好的可讀性。 但它是正確的。應該有效。

嘗試查看php_errors日誌文件,看看它是否觸發了某些內容。

<?php 

$str = ''; 
for($i = 11; $i <= 20; $i++) { 
    $str .= $i . ' '; 
} 

$ch = curl_init(); //http post to another server 
curl_setopt($ch, CURLOPT_URL   , 'http://xxxx'); 
curl_setopt($ch, CURLOPT_POST   , 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS , 'username=' . $username . '&password=' . $password . '&string=' . $str); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 

$server_output = curl_exec ($ch); 

print_r($server_output); 

curl_close($ch); 

@Stoic傳遞POSTFIELDS陣列確定所述Content-type頭=多。可以改變回應。

+0

「google.com」何時開始接受用戶名和密碼? –

+0

沒有實際意義 –

0

我明白你的代碼是正確的,但你可以使用下面的函數作爲幫手:

<?php 
$url = "http://xxxx"; 
$str = implode(" ", range(11,20)); 
$data = array("username" => $username, "password" => $password, "string" => $str); 

$server_output = processURL($url, $data); 
print_r($server_output); 

function processURL($url, $data = array()){ 
    $ch=curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_POST, 1); 
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 
    $response = curl_exec ($ch); 
    curl_close ($ch); 
    return $response; 
} 

需要注意的是,我不明白,使用rangefor循環慢了一點,但我喜歡它的可讀性和更乾淨的代碼:)

相關問題