php get html from url
How do I get text from a website using PHP?
So, I’m working on a PHP script, and part of it needs to be able to query a website, then get text from it.
First off, I need to be able to query a certain website URL, then I need to be able to get text from the text from that website after the query, and be able to return that text out of the function.
How would I query the website and get the text from it?
7 Answers 7
That will get you the source of the web page.
You probably want something a bit more complete though, so look into cURL, for better error handling, and setting user-agent, and what not.
From there, if you want the text only, you are going to have to parse the page. For that, see: How do you parse and process HTML/XML in PHP?
file_get_contents Example:
Basic cURL Example:
If you have Curl installed, use it. Otherwise:
Then you need to search through the string for the text you want. How you do that depends on the website, and the text you’re trying to read.
you need to use CURL. You can get some samples here
If you want more control, use cURL. Otherwise: file_get_contents..
Can this be done by getting all of the content from the webpage utilizing methods already listed above, and then using regex to remove all characters between open and closed brackets?
A page that looks like this:
Would then become this after regex:
And because we want to remove all of the code in between various tags such as the [style] tag, we could then first use regex to remove all characters between [style and /style] so that we are just left with:
Would this work then? Please reply if you think it would or if you foresee errors as I would like to create a tool with this parsing.
How to send a GET request from PHP?
I’m planning to use PHP for a simple requirement. I need to download a XML content from a URL, for which I need to send HTTP GET request to that URL.
How do I do it in PHP?
8 Answers 8
For anything more complex, I’d use cURL.
For more advanced GET/POST requests, you can install the CURL library (http://us3.php.net/curl):
http_get should do the trick. The advantages of http_get over file_get_contents include the ability to view HTTP headers, access request details, and control the connection timeout.
Remember that if you are using a proxy you need to do a little trick in your php code:
Depending on whether your php setup allows fopen on URLs, you could also simply fopen the url with the get arguments in the string (such as http://example.com?variable=value )
I like using fsockopen open for this.
In the other hand, using REST API of other servers are very popular in PHP. Suppose you are looking for a way to redirect some HTTP requests into the other server (for example getting an xml file). Here is a PHP package to help you:
So, getting the xml file:
Not the answer you’re looking for? Browse other questions tagged php http get or ask your own question.
Linked
Related
Hot Network Questions
Subscribe to RSS
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2021.9.17.40238
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.
How to get parameters from a URL string?
How can I get only the email parameter from these URLs/values?
Please note that I am not getting these strings from browser address bar.
13 Answers 13
You can use the parse_url() and parse_str() for that.
will extract the emails from urls.
Use the parse_url() and parse_str() methods. parse_url() will parse a URL string into an associative array of its parts. Since you only want a single part of the URL, you can use a shortcut to return a string value with just the part you want. Next, parse_str() will create variables for each of the parameters in the query string. I don’t like polluting the current context, so providing a second parameter puts all the variables into an associative array.
As mentioned in other answer, best solution is using
parse_url()
The parse_url() parse URL and return its components that you can get query string using query key. Then you should use parse_str() that parse query string and return values into variable.
Also you can do this work using regex.
preg_match()
You can use preg_match() to get specific value of query string from URL.
preg_replace()
Also you can use preg_replace() to do this work in one line!
I created function from @Ruel answer. You can use this:
This is working great for me using php
A much more secure answer that I’m surprised is not mentioned here yet:
So in the case of the question you can use this to get an email value from the URL get parameters:
$email = filter_input( INPUT_GET, ’email’, FILTER_SANITIZE_EMAIL );
Might as well get into the habit of grabbing variables this way.
How do I get the base URL with PHP?
How do I get http://127.0.0.1/test_website/ with PHP?
I tried something like these, but none of them worked.
23 Answers 23
If you plan on using https, you can use this:
NOTE: If you’re depending on the HTTP_HOST key (which contains user input), you still have to make some cleanup, remove spaces, commas, carriage return, etc. Anything that is not a valid character for a domain. Check the PHP builtin parse_url function for an example.
Function adjusted to execute without warnings:
Fun ‘base_url’ snippet!
will create output like this :
and if this script works fine.
Try this. It works for me.
Note: This is an expansion of the answer provided by maček above. (Credit where credit is due.)
Edited at @user3832931 ‘s answer to include server port..
This is the best method i think so.
I used it to echo the base url of my site to link my css.
I had the same question as the OP, but maybe a different requirement. I created this function.
. which, incidentally, I use to help create absolute URLs that should be used for redirecting.
and you get something like
Just test and get the result.
.htaccess
index.php
Now I use this in the base tag of the template (in the head section of the page):
So if the variable was not empty, we use it. Otherwise fallback to / as default base path.
Based on the environment the base url will always be correct. I use / as the base url on local and production websites. But /foldername/ for on the staging environment.
Get HTML code using JavaScript with a URL
I am trying to get the source code of HTML by using an XMLHttpRequest with a URL. How can I do that?
I am new to programming and I am not too sure how can I do it without jQuery.
7 Answers 7
This data is your HTML.
Without jQuery (just JavaScript):
There is a tutorial on how to use Ajax here: https://www.w3schools.com/xml/ajax_intro.asp
This is an example code taken from that tutorial:
You can use fetch to do that:
Asynchronous with arrow function version:
Edit: doesnt work yet.
Add this to your JS:
It saves the source of page.com to variable ‘src’
First, you must know that you will never be able to get the source code of a page that is not on the same domain as your page in javascript. (See http://en.wikipedia.org/wiki/Same_origin_policy).
In PHP, this is how you do it:
In javascript, there is three ways :