A cheap way of debugging this is to use echo
to see what you're passing in to php:
echo "$parse = parse_url('$uri'); echo $parse['$key']"
shows
= parse_url('http://www.google.com?key=value'); echo ['host']
You're already using $uri
to mean "the value of the shell variable uri", so it's not surprising that $parse
is also considered a shell variable and expanded to its value (unset, nothing).
Use \$
when you want a literal dollar sign in your double quoted string:
value=$(php -r "\$parse = parse_url('$uri'); echo \$parse['$key']")