douyin8809 2016-08-31 08:19
浏览 150
已采纳

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

Sry for english. I have problem with this code:

$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 is "Gamma 2 Case", $polaczenie is object from mysqli, $connector curl object, when I replace this line:

curl_setopt($connector, CURLOPT_URL, $steam);

to this:

curl_setopt($connector, CURLOPT_URL,"https://steamcommunity.com/market/listings/730/Gamma 2 Case" 

everything is good. What I must do? PS When I try with first code this return "https://steamcommunity.com/market/listings/730/Gamma 2 Case <!---->"

  • 写回答

1条回答 默认 最新

  • drwdvftp423507 2016-08-31 08:45
    关注

    you need to use rawurlencode() to encode " " in the last part of the URL. Use it on $resultat:

    $rezultat=rawurlencode($rezultat['name']);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?