How to grab data from an external URL [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have an application where a url will be generated from website #1, after a user signs up.
(i.e. user will get their own url: http://www.example.com/?affid=12345)
I need to grab the numbers in the above url by using PHP, but I don't understand how this would work. How can I grab the actual numbers from the url above using PHP?
I should also mention that the website where I will generating PHP code is not on the same server as the URL above.

Use parse_url and parse_str for that:
$url = 'http://www.example.com/?affid=12345';
$query = parse_url($url, PHP_URL_QUERY);
parse_str($query, $vars);
echo $vars['affid']; // outputs 12345

To get the number i.e 12345 in this URL http://www.example.com/?affid=12345, use $_GET or $_REQUEST which will give you an associative array. you can use $_GET and $_REQUEST throught the application, while using $_GET OR $_REQUEST you must clean before using in your code.
echo $_GET['affid'];
OR
echo $_REQUEST['affid'];

You can use parse_url function to extract the query
<?php
$url = 'http://www.example.com/?affid=12345';
$parsed_url = parse_url($url);
echo $parsed_url['query'];
//print : affid=12345
?>
After that, you can parse the result with parse_str function
<?php
parse_str($parsed_url['query'],$result);
var_dump($result);
//print array(1) { ["affid"]=> string(5) "12345" }
echo $values[0]
//print 12345
?>

Related

Access to POST vars in php with string name [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Hi, I have a problem when try access to $_POST vars in php. I have a combo with this name "c012". Well, I send the form with this var, and I have checked this var is send ok, and when I try access with this code, where $var1, $var2 and $var3 are numbers:
$var1 = 0;
$var2 = 1;
$var3 = 2;
$pointer_combo = "c".$var1.$var2.$var3;
echo $_POST['$pointer_combo'];
Don't show anything, but if I try this:
echo $_POST['c012'];
Works, and show the value. Whats the problem with code above?
If you are using a dynamic index (index value stored in a variable), you don't need the quotes.
Try this:
echo $_POST[$pointer_combo];
PHP won't do variable substitution if the value is in single quotes. Only double quotes or no quotes. So
echo $_POST[$pointer_combo];
Would work, as would:
echo $_POST["$pointer_combo"];
(But obviously in that second example there isn't much point in the quotes being there!)
Lose the quotes:
$_POST[$pointer_combo];

Get part of the current url PHP [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How i cant get a specific part of the current url? for example, my current url is:
http://something.com/index.php?path=/something1/something2/something3/
Well, i need to print something2 with php.
Thanks!
You use the explode function in PHP to separate the URL by the first parameter (in this case a forward slash). To achieve your goal you could use;
$url = "http://something.com/index.php?path=/something1/something2/something3/";
$parts = explode('/', $url);
$value = $parts[count($parts) - 2];
All these other example seem to focus on your exact example. My guess is that you need a more flexible way of doing this, as the explode-only approach is very fragile if your URL changes and you still need to get data out of path parameter in query string.
I will point out the parse_url() and parse_str() functions to you.
// your URL string
$url = 'http://something.com/index.php?path=/something1/something2/something3/';
// get the query string (which holds your data)
$query_string = parse_url($url, PHP_URL_QUERY);
// load the parameters in the query string into an array
$param_array = array();
parse_str($query_string, $param_array);
// now you can look in the array to deal with whatever parameter you find useful. In this case 'path'
$path = $param_array['path'];
// now $path holds something like '/something1/something2/something3/'
// you can use explode or whatever else you like to get at this value.
$path_parts = explode('/', trim($path, '/'));
// see the value you are interested in
var_dump($path_parts);
You could do something like this:
$url = explode('/', 'http://something.com/index.php?path=/something1/something2/something3/');
echo $url[5];

Dynamically replacing # and # like Twitter [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
For my Laravel-based site, I need to find # and # within text content and replace it with a URL, such as a URL pointing at a user's Twitter page. How can I:
reliably find these strings within text portions of the HTML
replace found instances with a URL
The code for it is vast. You will have to use ajax here, in the textarea/textbox you will have to use "onkeyup" event, every key pressed have to be compared with "#" or "#" then the next character right after "#" has to be searched in the database.
So lets saw the user has typed "#A" till now and the user aims to type "#Ankur" Then as soon as "A" is typed the ajax script will start searching for users in the database and it is retrieved with the name, url and you just have to echo it on the screen.
THis is what you are looking for.. https://stackoverflow.com/a/4277114/829533
$strTweet = preg_replace('/(^|\s)#(\w*[a-zA-Z_]+\w*)/', '\1#\2', $strTweet);
And https://stackoverflow.com/a/4766219/829533
$input = preg_replace('/(?<=^|\s)#([a-z0-9_]+)/i', '#$1', $input);
Using regex it's rather simple. I would make one function that takes a prefix and replacement, like so.
function tweetReplaceObjects($input, $prefix, $replacement) {
return preg_replace("/$prefix([A-Za-z0-9_-])/", $replacment, $input);
}
An example usage would be something like this.
$text = 'hey look, it\'s #stackoverflow over there';
// expected output:
// hey look, it's stackoverflow over there
echo tweetReplaceObjects($text, '#', '$1');

How do i extract a number from a url using PHP [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have an SMF website and i'm actually trying to get some header information which includes the title of a particular thread, the url but i've been finding it difficult to get the unique link affixed to the url using PHP.
Here's the url: http://example.com/index.php?topic=6449.msg6858
I'm actually looking for a way to extract the number 6449, I've tried to use the php GET function but it doesn't work.
$parts = explode('.', $_GET['topic']);
echo $parts[0];
// PHP 5.4+
echo explode('.', $_GET['topic'])[0];
See it in action
This would work, too
echo (int) $_GET['topic'];
See it in action
You want to use a combination of substr and strpos (to find the first occurence of a period)
$number = substr($_GET['topic'], 0, strpos($_GET['topic'], '.'));
// 6449
$arr = array();
if (preg_match("/([\\d]+)([.]{1}msg[\\d]+)/", $_GET["topic"], $arr) == 1) {
echo $arr[1];
} else {
trigger_error("not found", E_USER_ERROR);
}

Get the last part of the URI [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
if i output my URI i also get the folder in which it is located at.
echo $_SERVER['REQUEST_URI']; //outputs /abcgetoutofjail/admin.php?make_account
im on localhost right now, and the website is under folder in htdocs abcgetoutofjail
i need only admin.php?make_account
i need the last part of the url in any way
how can i achieve that with either another way of getting uri or using a string function to cut SERVER URI
function DescriptionAndTitle($uri)
{
echo $uri;
}
In Php, you can achieve this using explode & count function.
$uri = '/some-dir/yourpage.php?q=bogus&n=10';
$uri = explode('/', $uri);
$len = count($uri);
print $uri[$len-1];
To get the last part of a URL I usually do something like the following:
$url = parse_url($_SERVER['REQUEST_URI']);
$partsofurl = explode("/", $url['path']);
$partyouwant = end($partsofurl);
$needed_part = end(explode('/', $_SERVER['REQUEST_URI']));
I believe this is what you're looking for:
$dir = explode('/', dirname($_SERVER['PHP_SELF']));
$dir = '/'.end($dir);
echo $dir;

Categories