Send parameters using php file_get_content function
Dear all,
How i send parameters using php file_get_content function.
$id='123';
$name='blah';
$response=file_get_contents('http://localhost/abc/abc.php?id=$id&name=$name');
echo $response;
I need to send the $id and name value to abc.php page.Here the value passing does not working.also i chk ?id="$id"&name="$name" value.It's also not working. But the straite parameter works.say-
$response=file_get_contents('http://localhost/abc/abc.php?id=123&name=blah');
Now is t开发者_Go百科heir any Kind heart who can help me to send the 2 parameters $id and $name to abc.php?
Thanks
riadSingle quotes inhibit variable substitution.
$response=file_get_contents("http://localhost/abc/abc.php?id=$id&name=$name");
Don't forget to URL-encode all parameters though.
If you want to enable variable substitution in a string, you have to use double quotes:
$response=file_get_contents("http://localhost/abc/abc.php?id=$id&name=$name");
Use double quotes instead of single quotes. i.e "http://localhost/abc/abc.php?id=$id&name=$name"
.
Try
$response = file_get_contents(sprintf("http://localhost/abc/abc.php?id=%s&name=%s", $id, $name));
A safer way is to use http_build_query which escapes the variables properly:
$response=file_get_contents('http://localhost/abc/abc.php?'.http_build_query(['id'=>$id, 'name'=>$name]));
put this thing in double quotes:
$response=file_get_contents("http://localhost/abc/abc.php?id=$id&name=$name");
Thanks.
精彩评论