[PHP CURL]如何从带有变量的url中获取数据?

时间:2016-08-31 08:19:21

标签: php curl

讽刺英语。 我对此代码有疑问:

$rezultat=$polaczenie->query("SELECT * FROM items LIMIT 0,1");
$rezultat=mysqli_fetch_assoc($rezultat);
$rezultat=$rezultat['name'];
$connector = curl_init();
$steam='https://steamcommunity.com/market/listings/730/$rezultat';
$steam=str_replace('$rezultat',$rezultat,$steam);
echo $steam;
curl_setopt($connector, CURLOPT_URL, $steam);
curl_setopt($connector, CURLOPT_FOLLOWLOCATION, FALSE);
curl_setopt($connector, CURLOPT_RETURNTRANSFER, 1);
$out = curl_exec($connector);
echo "<!--".$out."-->";
curl_close($connector);

$ rezultat是“Gamma 2 Case”,$ polaczenie是来自mysqli,$ connector curl对象的对象,当我替换此行时:

curl_setopt($connector, CURLOPT_URL, $steam);

到此:

curl_setopt($connector, CURLOPT_URL,"https://steamcommunity.com/market/listings/730/Gamma 2 Case" 
一切都很好。我应该做什么? PS 当我尝试第一个代码时,返回“https://steamcommunity.com/market/listings/730/Gamma 2 Case&lt;!----&gt;”

1 个答案:

答案 0 :(得分:0)

您需要在网址的最后部分使用rawurlencode()来编码“”。在$resultat

上使用它
$rezultat=rawurlencode($rezultat['name']);