can not use file_get_contents of dyanmically created location
I am trying to use file_get_contents() to get the html from a page.
the following works great:file_get_contents('http://www.mypage.com?Title=Title')
but the following causes an error:
$Title = 'Title';
file_get_contents("http://www.mypage.com?T开发者_JAVA技巧itle=$Title")
The error is:
Bad Request
Your browser sent a request that this server could not understand.
The request line contained invalid characters following the protocol string.
Apache/1.3.41 Server at eiit.org Port 80
Does anyone know why?
You are using a string with single-quotes ; and there is no variable interpolation with single-quotes.
Which means the URL you're trying to fetch is http://www.mypage.com?Title=$Title
, and not http://www.mypage.com?Title=Title
.
You should use a double-quoted string, to have variable interpolation :
$Title = 'Title';
file_get_contents("http://www.mypage.com?Title=$Title");
If this still doesn't work :
- Check if your URL is OK : instead of directly passing it to
file_get_contents
, store it in a variable, and echo it -- just to be sure it's right. - Why is there no page-name in your URL ?
- You have the domain-name :
www.mypage.com
- And a parameter+value :
Title=Title
- But no file/page ? i.e., why don't you have something like
http://www.mypage.com/index.php?Title=$Title
? Or evenhttp://www.mypage.com/?Title=$Title
?
- You have the domain-name :
- You might have to urlencode the values you're passing as parameters in the URL.
Variables inside single quotes dont get interpolated; try:
$Title = 'Title';
file_get_contents("http://www.mypage.com?Title=$Title")
Have you tried http://www.mypage.com/?Title=$Title
? The slash after the domain name is the path - you must always have a path in an HTTP request.
if i don't if you have resolved your problem, but another way you can try. (although i don't think it matters)
$Title = 'Title';
$result=file_get_contents("http://www.mypage.com?Title=".$Title)
精彩评论