Bifalgorithm post · Computer science one · Closing Files

How can PHP use URLs as file names?

How can PHP use URLs as file names? (Computer science one · Closing Files) — "44.2.1. Using URLs A nice feature of PHP is that you can use URLs as file names t…

Ideas in this flowchart

  • Using URLs as file names in PHP
  • PHP can use URLs as file names for reading and writing
  • Reading from a URL connects to a remote resource and downloads its contents
  • Downloaded contents may be HTML, XML, or JSON data
  • Writing to a URL can post data to a web page and receive a response
  • Example: downloading a webpage from http://cse.unl.edu
  • Open the webpage URL for reading with fopen
  • Initialize an empty string to store the downloaded contents
  • Read and append lines until the end of the file is reached
  • Alternatively, download the complete webpage with file_get_contents
Feed/

How can PHP use URLs as file names?

No cover

Channel

Computer science one

1 page

"44.2.1. Using URLs A nice feature of PHP is that you can use URLs as file names to read and write to a URL. “Reading” from a URL mean connecting to a remote resource, such as a webservice and downloading its contents (which may be HTML, XML or JSON data). Writing to a URL may be used to post data to a web page in order to receive a response. As an example, we can download a webpage in PHP as follows.\n\n1 $h = fopen(\"http://cse.unl.edu\", \"r\");\n\n2 $contents = \"\";\n\n3 while(!feof($h)) {\n\n4 $contents .= fgets($h);\n\n5 }\n\n6\n\n7 //or just:\n\n8 $contents = file_get_contents(\"http://cse.unl.edu\");"

Flowchart preview
👍
0
0 comments0 shares

Sign in to view and post comments.