将 2 个不同的变量插入到一个 cURL 行中

Insert 2 different variables into one cURL line

我想取一个变量的 1 个随机值并将其插入到 cURL 命令中。 此外,在添加这个随机变量的值之后,cURL 应该 运行 具有这个值的函数,并且在函数的最后,我希望它向第一个添加一个不同的值所以函数将是完成了就会有结果了。

$kinds = array(
    "http://fruits.com/select.php?=",
    "http://vegtables.com/select.php?=",
);
$random = array_rand($kinds);

function get_fruits($fruit){    
   //get content
    $ch = curl_init();
    $timeout = 5;  
    curl_setopt($ch,CURLOPT_URL,$kinds[$random].$fruit);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); 
    curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);  
    $content = curl_exec($ch); 
    curl_close($ch);
    return $content;
}

$test = get_fruits('apple');
echo $test;  

$test 出现空值。空白的。 如您所见,它采用随机 $kinds,然后将函数后给出的 $fruit 值添加给他。

我认为是因为函数的第二行:

curl_setopt($ch,CURLOPT_URL,$kinds[$random].$fruit);  

因为如果我改变

$kinds[$random].$fruit 

'http://fruits.com/select.php?='

一切正常。 我的意思是,当我使用下一种方式时:

curl_setopt($ch,CURLOPT_URL,'http://fruits.com/select.php?='.$fruit);  

一切正常。

但我不想定义'http://frutis.com',我想从 array_rand 中给出的 url 中随机取一个 url ] 函数。

我不知道该怎么办。

非常感谢。 我已经尝试过以下方法:

curl_setopt($ch,CURLOPT_URL,"$kinds[$random]".$fruit); 
curl_setopt($ch,CURLOPT_URL,$kinds[$random].$fruit); 

curl_setopt($ch,CURLOPT_URL,$kinds[$random]."/select.php?=".$fruit); //(when I defined the $kinds as the url only without the select.php)

您的 $kinds 和 $random 变量是在 get_fruits() 函数之外定义的,因此要么在函数内部对它们使用关键字 global,要么将它们发送到函数。

function get_fruits($fruit){    
    global $kinds, $random;
    ...
}

function get_fruits($fruit, $kinds, $random){
    ...
}
$test = get_fruits('apple', $kinds, $random);

此外,您可能只想向函数发送 1 个参数,url,而不是同时发送数组和索引。