PHP file_get_contents() returns “failed to open stream: HTTP request failed!”

前端 未结 11 871
名媛妹妹
名媛妹妹 2020-11-22 13:03

I am having problems calling a url from PHP code. I need to call a service using a query string from my PHP code. If I type the url into a browser, it works ok, but if I use

11条回答
  •  日久生厌
    2020-11-22 13:43

    You basically are required to send some information with the request.

    Try this,

    $opts = array('http'=>array('header' => "User-Agent:MyAgent/1.0\r\n")); 
    //Basically adding headers to the request
    $context = stream_context_create($opts);
    $html = file_get_contents($url,false,$context);
    $html = htmlspecialchars($html);
    

    This worked out for me

提交回复
热议问题