찾다
php教程php手册file_get_contents(url): failed to open stream的解决方案

www.2cto.com:摘自英文网站

提问者:

Hello Everyone;

 

I am having a php file that gets the contents from a URL, i am getting the failure message 

 

 Collapse | Copy CodeWarning: file_get_contents(http://xxxxxx): failed to open stream: HTTP request failed! in xxxx.php on line xx 

i tried so many online solutions but its still not working. here is the code

 

 Collapse | Copy Code            $cryptpass = rawurlencode(crypt($pc['pcpassword']));

            $url = "http://" . $pc['pcname']."/Reports/ReportList.php?&username={$pc['pcusername']}&cryptpass=$cryptpass&noredir=1";

            $parsed_list = read_general_list($url, false); 

 Collapse | Copy Codefunction read_general_list($url, $make_assoc = false)

    {

        $compressed_data = file_get_contents($url);

    }

 

 

 

$compressed_data is always null and it throws an error:

 

Warning: file_get_contents(http://xxxxxx): failed to open stream: HTTP request failed! in xxxx.php on line xx

 

Any suggestions please? 

答复:

I am sorry, i never updated this question completely. May be if some one is still looking for an answer.

 

This has worked for me. This is the equivalent function for File_get_contents, but can handle large amount of data. I found this solution online.

 

 Collapse | Copy Code      function file_get_contents_curl($url) {

      $ch = curl_init();

      curl_setopt($ch, CURLOPT_HEADER, 0);

      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Set curl to return the data instead of printing it to the browser.

      curl_setopt($ch, CURLOPT_URL, $url);

      $data = curl_exec($ch);

      curl_close($ch);

      return $data;

      }

 

第二个解答:
 

As the error message says, the stream (URL) requested cannot be opened. There are many possible reasons for this:

1. base URL is bad. $pc['pcname']

2. username and/or password are bad

3. username/password do not have permission on the server

4. Your system cannot reach the server (firewall, PHP permissions, ...)

4. ...

 

I would use the following strategy to debug:

1. Dump $url and write it down.

2. Use a browser with debug tools (eg Firefox/Firebug) and try to access that URL.

3. Look at the headers returned to see what error the server reports (if any).

4. Think about why that error is returned...

 

Cheers,

Peter

 

If this answers your question, vote and mark it accepted.


성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.

핫 AI 도구

Undresser.AI Undress

Undresser.AI Undress

사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover

AI Clothes Remover

사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool

Undress AI Tool

무료로 이미지를 벗다

Clothoff.io

Clothoff.io

AI 옷 제거제

AI Hentai Generator

AI Hentai Generator

AI Hentai를 무료로 생성하십시오.

인기 기사

R.E.P.O. 에너지 결정과 그들이하는 일 (노란색 크리스탈)
3 몇 주 전By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 최고의 그래픽 설정
3 몇 주 전By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 아무도들을 수없는 경우 오디오를 수정하는 방법
3 몇 주 전By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25 : Myrise에서 모든 것을 잠금 해제하는 방법
4 몇 주 전By尊渡假赌尊渡假赌尊渡假赌

뜨거운 도구

스튜디오 13.0.1 보내기

스튜디오 13.0.1 보내기

강력한 PHP 통합 개발 환경

Dreamweaver Mac版

Dreamweaver Mac版

시각적 웹 개발 도구

WebStorm Mac 버전

WebStorm Mac 버전

유용한 JavaScript 개발 도구

SublimeText3 Mac 버전

SublimeText3 Mac 버전

신 수준의 코드 편집 소프트웨어(SublimeText3)

mPDF

mPDF

mPDF는 UTF-8로 인코딩된 HTML에서 PDF 파일을 생성할 수 있는 PHP 라이브러리입니다. 원저자인 Ian Back은 자신의 웹 사이트에서 "즉시" PDF 파일을 출력하고 다양한 언어를 처리하기 위해 mPDF를 작성했습니다. HTML2FPDF와 같은 원본 스크립트보다 유니코드 글꼴을 사용할 때 속도가 느리고 더 큰 파일을 생성하지만 CSS 스타일 등을 지원하고 많은 개선 사항이 있습니다. RTL(아랍어, 히브리어), CJK(중국어, 일본어, 한국어)를 포함한 거의 모든 언어를 지원합니다. 중첩된 블록 수준 요소(예: P, DIV)를 지원합니다.