dyanmically oluşturulan konumu file_get_contents kullanamazsınız

4 Cevap

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?Title=$Title")

Hata:

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

Herkes neden biliyor mu?

4 Cevap

Eğer tek-tırnak ile bir dize kullanarak; ve tek tırnak ile hiçbir değişken interpolasyon yoktur.

Hangi getirmek için çalışıyoruz URL http://www.mypage.com?Title=$Title, ve değil http://www.mypage.com?Title=Title anlamına gelir.

Değişken enterpolasyon için, bir çift tırnaklı dize kullanmanız gerekir:

$Title = 'Title';
file_get_contents("http://www.mypage.com?Title=$Title");


If this still doesn't work :

  • Sizin URL Tamam olup olmadığını denetleyin: yerine doğrudan file_get_contents, bir değişkende saklamak ve onu yankı geçirmeden - sadece doğru olduğundan emin olmak için.
  • Why is there no page-name in your URL ?
    • Sen etki alanı adı var: www.mypage.com
    • Ve bir parametre + değeri: Title=Title
    • Ancak dosya / sayfa no? yani, neden http://www.mypage.com/index.php?Title=$Title gibi bir şey yok mu? Hatta http://www.mypage.com/?Title=$Title?
  • Eğer URL parametre olarak geçiyoruz urlencode değerlerine sahip olabilir.

Tek tırnak içindeki değişkenler interpolated dont get; deneyin:

$Title = 'Title';
file_get_contents("http://www.mypage.com?Title=$Title")

Eğer çalıştı http://www.mypage.com/?Title=$Title? Var Alan adından sonra çizgi yolu - her zaman bir HTTP isteği bir yolu olmalı.

ben senin sorunun giderilmiş olup olmadığını bilmiyorum, ama eğer başka bir yol deneyebilirsiniz. (I sanmıyorum, ancak bu konularda)

$Title = 'Title';
$result=file_get_contents("http://www.mypage.com?Title=".$Title)