Here is the php.net listing for http_head.
The function prototype is
string http_head ( string $url [, array $options [, array &$info ]] )
A list of $options is here.
I want to use this to validate that a set of URLs is valid.
[url1, url2, url3]
Are there any options that should be set? Is any of the $info relevant to should I just make sure that false is not returned instead of a string?
You probably don't need to set any of the $options, unless you're behind a proxy server or need to do something unusual. You probably should look through them just in case.
It's not likely you need to mess with $info unless you're debugging, since this gives you more complete visibility into the request and response.
Sample code:
foreach ($urls as $url) {
$response = http_head($url);
if ($response !== false) {
# FIXME do something cool
} else {
# FIXME hey that url is broken!
}
}
Related
I need to create a an if statement that fires if there is something after a given string in the URL.
For example, if my string is 'Honda' I need to check if something appears after that word in the url, eg something like:
$brand = "honda";
if (url contains $brand . '/*'){
// Do something.
}
Example urls could be:
mysite.com/honda (which would fail the above)
mysite.com/cars/honda (which would fail the above)
mysite.com/honda/civic (which would pass the above)
mysite.com/honda/accord (which would pass the above)
I know I can use strpos() to detect if the string is within the URL, but how can I detect if anything comes after that?
Use a regular expression that looks for the $brand at the end of the string.
if (! preg_match("/{$brand}$/", $url)) {
// ...
}
If you need to check if the $brand actually appears in the URL before running your end of string check:
if (strpos($brand, $url) !== false && ! preg_match("/{$brand}$/", $url)) {
// ...
}
I was thinking if there is a way to hide part of the url in PHP/ Zend Framework 2. Something like this:
sitename.com/something/?inviter=1234&id=1
But I'd like to hide the part with the &id=1 somehow, so that when the url is copied and entered by the user, it would look like this:
sitename.com/something/?inviter=1234
And on the other side I can do something like this:
$id = $_GET["id"])
Is this possible to do, if so, how? Maybe there is something close to what I'm looking for to achieve?
You can hide it only with Cookie or Session techniques. But it will work only for one user during one session.
You can parse and rebuild the url using parse_url, http_build_url, with parse_str, and http_build_str.
For example:
/**
* Transform a url using a whitelist of query-string keys
*/
function transformUrlKeepQueryKeys($url, array $whitelist)
{
// Break the given url into parts
$parts = parse_url($url);
// Break the parts into key-value pairs
$query = $parts['query'];
parse_str($query, $queryParts);
// Unset all unwanted keys
foreach (array_keys($queryParts) as $k) {
if (!in_array($k, $whitelist)) {
unset($queryParts[$k]);
}
}
// rebuild the url
$parts['query'] = http_build_query($queryParts);
// return
return http_build_url('', $parts);
}
Invocation should be:
$url = 'http://sitename.com/something/?inviter=1234&id=1';
$whitelist = [
'inviter'
];
$expectedUrl = 'http://sitename.com/something/?inviter=1234';
$actualUrl = transformUrlKeepQueryKeys($url, $whitelist);
assert($expectedUrl == $actualUrl);
Alternatively, you could implement something similar using a blacklist of keys to remove.
The only problem with this is that the function http_build_url is not included in core PHP, but is part of the PECL HTTP extension. If you are unable to install that extension in your environment, then you can use a pure PHP implementation of that function, for example here.
I want remove a parameter from a URL:
$linkExample1='https://stackoverflow.com/?name=alaa&counter=1';
$linkExample2='https://stackoverflow.com/?counter=4&star=5';
I am trying to get this result:
https://stackoverflow.com/?name=alaa&
https://stackoverflow.com/?&star=5
I am trying to do it using preg_replace, but I've no idea how it can be done.
$link = preg_replace('~(\?|&)counter=[^&]*~','$1',$link);
Relying on regular expressions can screw things up sometimes..
You should use, the parse_url() function which breaks up the entire URL and presents it to you as an associative array.
Once you have that array, you can edit it as you wish and remove parameters.
Once, completed, use the http_build_url() function to rebuild the URL with the changes made.
Check the docs here..
Parse_Url Http_build_query()
EDIT
Whoops, forgot to mention. After you get the parameter string, youll obviously need to separate the parameters as individual ones. For this you can supply the string as input to the parse_str() function.
You can even use explode() with & as the delimeter to get this done.
I would recommend using a combination of parse_url() and http_build_query().
Handle it correctly! !
remove_query('http://example.com/?a=valueWith**&**inside&b=value');
Code:
function remove_query($url, $which_argument=false){
return preg_replace( '/'. ($which_argument ? '(\&|)'.$which_argument.'(\=(.*?)((?=&(?!amp\;))|$)|(.*?)\b)' : '(\?.*)').'/i' , '', $url);
}
A code example how I would grab a requested URL and remove a parameter called "name", then reload the page:
$url = $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; //complete url
$parts = parse_url($url);
parse_str($parts['query'], $query); //grab the query part
unset($query['name']); //remove a parameter from query
$dest_query = http_build_query($query); //rebuild new query
$dest_url=(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http").'://'.$parts['path'].'?'.$dest_query; //add query to host
header("Location: ".$dest_url); //reload page
parse_url() and parse_str() are buggy. Regular expressions can work but have the tendency to break. If you want to correctly deconstruct, make changes, and then reconstruct a URL, you should look at:
http://barebonescms.com/documentation/ultimate_web_scraper_toolkit/
ExtractURL() generates parse_url()-like output but does much more (and does it right). CondenseURL() takes an array from ExtractURL() and constructs a new URL from the information. Both functions are in the 'support/http.php' file.
Years later...
$_GET can be manipulated like any other array in PHP. Simply unset the key and create the http query using the http_build_query function.
// Populate _GET with sample data...
$_GET = array(
'value_a' => "A",
'key_to_remove' => "Don't delete me bro!",
'value_b' => "B"
);
// Should output everything...
// "value_a=A&key_to_remove=Don%27t+delete+me+bro%21&value_b=B"
echo "\n".http_build_query( $_GET );
// Remove the key from _GET...
unset( $_GET[ 'key_to_remove' ] );
// Should output everything else...
// "value_a=A&value_b=B"
echo "\n".http_build_query( $_GET );
This is working for me:
function removeParameterFromUrl($url, $key)
{
$parsed = parse_url($url);
$path = $parsed['path'];
unset($_GET[$key]);
if(!empty(http_build_query($_GET))){
return $path .'?'. http_build_query($_GET);
} else return $path;
}
I just created an account on 500px.com, and would like to use their API: http://developer.500px.com/
But it looks like it cant return JSONP, only JSON.
Is there any way to make a php file that you can post a url to -> make it convert the response from the api from JSON to JSONP, so i can handle it on the client side?
I havent been writing any PHP in a long time, so any help is appreciated. Hope you get the idea, otherwise ill elaborate. Thanks
Sure you can. The only difference between JSON and JSONP is that JSONP is wrapped with a function name;
{ "x": "hello" } // JSON
foo({ "x": "hello" }); // JSONP.
In it's simplest form you can end up with something like this;
<?php echo $_GET['callback']; ?>(<?php echo file_get_contents($_GET['endpoint']); ?>);
and you'd expect clients to use it like this;
http://yourserver.com/proxy.php?endpoint=https%3A%2F%2Fapi.500px.com%2Fv1%2Foauth%2Fauthorize&callback=foo
Note the encoded URL and the addition of the callback parameter to know which function to invoke.
Of course, you'll need to valid the input; check the existance of the parameters, check the endpoint passed isn't malicious etc. You might want to also cache the responses on your server to stop you reaching any limits imposed on the API.
Something more resilient against malicious input could look like;
// checks for existence, and checks the input isn't an array
function getAsString($param) {
if (is_null($_GET[$param]) || !is_string($_GET[$param])) {
return '';
}
return $_GET[$param];
}
$endpoint = getAsString('endpoint');
// check callback is only alpha-numeric characters
$callback = preg_replace('/[^\w\d]/', '', getAsString('callback'));
// check the endpoint is for the 500px API
$is_api = (strpos($endpoint, 'https://api.500px.com') === 0);
if (strlen($callback) > 0 && $is_api) {
echo $callback . '(' . file_get_contents($endpoint) . ')'
} else {
// handle error
}
This question already has answers here:
Get content from a url using php
(3 answers)
Closed 7 years ago.
In php I need to get the contents of a url (source) search for a string "maybe baby love you" and if it does not contain this then do x.
Just read the contents of the page as you would read a file. PHP does the connection stuff for you. Then just look for the string via regex or simple string comparison.
$url = 'http://my.url.com/';
$data = file_get_contents( $url );
if ( strpos( 'maybe baby love you', $data ) === false )
{
// do something
}
//The Answer No 3 Is good But a small Mistake in the function strpos() I have correction the code bellow.
$url = 'http://my.url.com/';
$data = file_get_contents( $url );
if ( strpos($data,'maybe baby love you' ) === false )
{
// do something
}
Assuming fopen URL Wrappers are on ...
$string = file_get_contents('http://example.com/file.html');
if(strpos ('maybe baby love you', $string) === false){
//do X
}
If fopen URL wrappers are not enabled, you may be able to use the curl module (see http://www.php.net/curl )
Curl also gives you the ability to deal with authenticated pages, redirects, etc.