PHP Curl: how to pass a variable ? - a beginner question;-) - php

good evening dear community.
I need a starting-point! A German DB that collects all the data from all German Foundations...
see this url here - just click it to see a search page
<?php
//
// The PHP curl module supports the received page to be returned in a variable
// if told.
//
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://www.suche.stiftungen.org/index.php?strg=87_124&baseID=129&PHPSESSID=efd25e89a4986cb0981c602fc7c68780");
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$result=curl_exec ($ch);
curl_close ($ch);
?>
Question: Here we find all Foundations in Germany: : 8074 different foundations: You get the full results if you choose % as wildcard in the Search-field. How to do this with PHP: i think that we have to do this with curl or with file_get_contents_ - those are the best methods for doing this: What do you think, personally. I am curious to get your ideas to know! please. lemme know what you think!? BTW - probably - the XPATH and DOM-Technique can be used too. I guess so!?
First approach: see this url here - just click it to see a search page
The way to get through this database is to search (in the search-field) combinations of letters eg "ac" and select search only titles. Then go through every pair of letters. If you still get too many results for a particular pair, use 3 letters. aca, acb,...If i would have a startingpoint i would be happy.
how to enlarge the above written php-code... how to get the variables of my approach into the php-programme...!?
I need to pass the variables as combinations of letters eg "ac" and select search only titles.
Then i have to parse the results - then i have to go through every pair of letters.
look forward to hear from you...
zero

If you want to use % as parameter value in an URL, you need to escape it with urlencode.

Related

curl_exec pulling wrong HTML vs string passed to curl_init

My curl request string is created programmatically with a base URL from a database and an integer which is the page number. The string for the URL is correct. If I call the curl request with the base URL copied in to the function, it works. If I call it with the programmatically created string (which is identical) the wrong page is called - it appears to be missing one of the parameters from the string (affiliation):
This has been running for years with no issue. The server is PHP 5.6. The string in the database is stored as varchar(500). I have tried TEXT as the type in the database. I have tried various ways of forcing a string in the function. The first $testurl below is copied and pasted from the second $testurl if I get the code to echo it before making the curl call.
//choose one of the $testurls to try
$testurl = 'https://log.concept2.com/challenges/holiday/2019/honorboard/200?country=0&state=&affiliation=1397&sort=distance&page=2';
//In this... $url comes from the database, $gotopage comes from a loop
$testurl = (string)($url . $gotopage);
$c = curl_init($testurl)
$str = curl_exec($c);
curl_close($c);
echo $str;
die;
The first $testurl returns what I want, a list of people in the same team.
The second $testurl returns a list of people belonging to any affiliation.
Edit
var_dump($testurl) gives:
string(134) "https://log.concept2.com/challenges/holiday/2019/honorboard/200?country=0&state=&affiliation=1397&sort=distance&page=2"
The full code is more of a challenge. The $GoToPage is an integer. $url is pulled from a database. The pertinent line is:
$url = $challenge['mb2_Challenges']['challenge_url'];
Where the $challenge is created from a mysqli SELECT... FROM... WHEN... query with SQL. mb2_Challenges is the table, challenge_url is the table.
For those who are as foolish as me, here is the answer:
The string from the script was urlencoded, so & instead of &. I was in too much of a rush to spot this. It just needed htmlspecialchars_decode.
Lesson learned: always look at the source!

Extract domain name from affiliate URL using PHP

Here is the format of affiliate URL I have http://tracking.vcommission.com/aff_c?offer_id=2119&&url=http%3A%2F%2Fwww.netmeds.com%2F%3Fsource_attribution%3DVC-CPS-Emails%26utm_source%3DVC-CPS-Emails%26utm_medium%3DCPS-Emails%26utm_campaign%3DEmails
If you see it has 2 URLs:
first URL: is for vcommission.com and
Second URL: netmeds.com
I have CSV file with lot of rows. Each rows may have different second URL. I wanted to get second URL for each rows. First URL is also not static as for different CSV, this would also different.
How can I get second URL?
Some basic string parsing like this should give you an idea.
$url='http://tracking.vcommission.com/aff_c?offer_id=2119&&url=http%3A%2F%2Fwww.netmeds.com%2F%3Fsource_attribution%3DVC-CPS-Emails%26utm_source%3DVC-CPS-Emails%26utm_medium%3DCPS-Emails%26utm_campaign%3DEmails';
list($u,$q)=explode('url=',urldecode($url));
$o=(object)parse_url($q);
echo $o->host;
A good way to find the domain for a URL is with parse_url
Unfortunately due to the way your data is stored this is not really an option however you may be able to use some sort of regex to find contained web addresses in the query string
<?php
$url = "http://tracking.vcommission.com/aff_c?offer_id=2119&&url=http%3A%2F%2Fwww.netmeds.com%2F%3Fsource_attribution%3DVC-CPS-Emails%26utm_source%3DVC-CPS-Emails%26utm_medium%3DCPS-Emails%26utm_campaign%3DEmails";
$p = parse_url($url);
$pattern = "/www[^%]*/";
preg_match($pattern, $p['query'], $result);
var_dump($result);
You may need to adjust the regex pattern based on how the other data presents itself.

Dealing with online newspaper headline link in PHP

I have seen on most online newspaper websites that when i click on a headline link, e.g. two thieves caught red handed, it normally opens a url like this: www.example.co.uk/news/two-thieves-caught-red-handed.
How do I deal with this url in php code, so that I can only pick the last part in the url. e.g. two-thieves-caught-red-handed. After that I want to work with this string.
I know how to deal with GET parameters like "www.example.co.uk/news/headline=two thieves caught red handed".
But I do not want to do it that way. Could you show me another way.
You can use the combination of explode and end functions for that
for example:
<?php
$url = "www.example.co.uk/news/two-thieves-caught-red-handed";
$url = explode('/', $url);
$end = end($url);
echo "$end";
?>
The code will result
two-thieves-caught-red-handed
You have several options in php to get the current url. For a detailed overview look here.
One would be to use $_SERVER[REQUEST_URI] and the use a string manipulation function for extraction of the parts you need.
Maybe this thread will help you too.

Pulling certain information from Json

I've recently been exploring the Xbox API over at XboxAPI.com to try and increase my knowledge and confidence of actually using API's and also using Json Data. I found some code on another question that I had a play around with and got it to give me something back, the code I am currently using is:
$url = 'https://xboxapi.com/v2/2745051201447500/presence';
$headers = array(
'X-AUTH: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
Visiting the page in the browser gives me the following:
{"xuid":2745051201447500,"state":"Online","devices":[{"type":"XboxOne","titles":[{"id":714681658,"name":"Home","placement":"Background","state":"Active","lastModified":"2014-10-07T22:02:34.821235Z"},{"id":446059611,"activity":{"richPresence":"In a Street Race."},"name":"Forza Horizon 2","placement":"Full","state":"Active","lastModified":"2014-10-07T22:02:34.821235Z"}]}]}
My question is, how do I pull certain bits of information out of the above? For example if I wanted to pull the "name" and just display that, how would I go about doing that? I've tried a couple of things including the following:
echo $result->devices[0]->type;
but that didn't work. I don't know how far off I am from the correct answer, but would appreciate any assistance.
Thanks
I suspect you will need some akin to json_decode (http://php.net/manual/en/function.json-decode.php)
:
$result = json_decode(curl_exec($ch);
echo $result->devices->titles->name;
Note: The above has not been tested.
First, do a json_decode like below which will put into an associate array.
Then, you would need also to make sure you use an index on those that are multidimensional as noted below.
And voilla - basically, you're missing the json_decode.
$result = json_decode(curl_exec($ch));
$result->devices[0]->type;
Ugh - also forgot
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
You're curl is not outputting to a string variable.
devices is an array (note the [] in the JSON), so you need
$result->devices[0]->type // XboxOne
^^^
The same will hold for titles - that's also an array and would need [] dereferencing in PHP. a var_dump($result) would show you a nicely formatted structured dump of the array, and show you you exactly what "path" you need to use to get any to any piece of data in it.

Jquery, ajax and the ampersand conundrum

I know that I should encodeURI any url passed to anything else, because I read this:
http://www.digitalbart.com/jquery-and-urlencode/
I want to share the current time of the current track I am listening to.
So I installed the excellent yoururls shortener.
And I have a bit of code that puts all the bits together, and makes the following:
track=2&time=967
As I don't want everyone seeing my private key, I have a little php file which takes the input, and appends the following, so it looks like this:
http://myshorten.example/yourls-api.php?signature=x&action=shorturl&format=simple&url=http://urltoshorten?track=2&time=967
So in the main page, I call the jquery of $("div.shorturl").load(loadall);
It then does a little bit of CURL and then shortener returns a nice short URL.
Like this:
$myurl='http://myshorten.example/yourls-api.php?signature=x&action=shorturl&format=simple&url=' . $theurl;
$ch = curl_init($myurl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
curl_close($ch);
if ($data === false) {
echo 'cURL failed';
exit;
}
echo $data;
All perfect.
Except... the URL which is shortened is always in the form of http://urltoshorten?track=2 - anything after the ampersand is shortened.
I have tried wrapping the whole URL in php's URLencode, I've wrapped the track=2&time=967 in both encodeURI and encodeURIComponent, I've evem tried wrapping the whole thing in one or both.
And still, the & breaks it, even though I can see the submitted url looks like track=1%26time%3D5 at the end.
If I paste this or even the "plain" version with the unencoded url either into the yoururls interface, or submit it to the yoururls via the api as a normal URL pasted into the location bar of the browser, again it works perfectly.
So it's not yoururls at fault, it seems like the url is being encoded properly, the only thing I can think of is CURL possibly?
Now at this point you might be thinking "why not replace the & with a * and then convert it back again?".
OK, so when the url is expanded, I get the values from
var track = $.getUrlVar('track');
var time = $.getUrlVar('time');
so I COULD lose the time var, then do a bit of finding on where the * is in track and then assume the rest of anything after * is the time, but it's a bit ugly, and more to the point, it's not really the correct way to do things.
If anyone could help me, it would be appreciated.
I have tried wrapping the whole URL in php's URLencode
That is indeed what you have to do (assuming by ‘URL’ you mean inner URL being passed as a component of the outer URL). Any time you put a value in a URL component, you need to URL-encode, whether the value you're setting is a URL or not.
$myurl='http://...?...&url='.rawurlencode($theurl);
(urlencode() is OK for query parameters like this, but rawurlencode() is also OK for path parts, so unless you really need spaces to look slightly prettier [+ vs %20], I'd go for rawurlencode() by default.)
This will give you a final URL like:
http://myshorten.example/yourls-api.php?signature=x&action=shorturl&format=simple&url=http%3A%2F%2Furltoshorten%3Ftrack%3D2%26time%3D967
Which you should be able to verify works. If it doesn't, there is something wrong with yourls-api.php.
I have tried wrapping the whole URL in php's URLencode, I've wrapped the track=2&time=967 in both encodeURI and encodeURIComponent, I've evem tried wrapping the whole thing in one or both. And still, the & breaks it, even though I can see the submitted url looks like track=1%26time%3D5 at the end.
Maybe an explanation of how HTTP variables work will help you out.
If I'm getting a page with the following variables and values:
var1 = Bruce Oxford
var2 = Brandy&Wine
var3 = ➋➌➔ (unicode chars)
We uri-encode the var name and the value of the var, ie:
var1 = Bruce+Oxford
var2 = Brandy%26Wine
var3 = %E2%9E%8B%E2%9E%8C%E2%9E%94
What we are not doing is encoding the delimiting charecters, so what the request data will look like for the above is:
?var1=Bruce+Oxford&var2=Brandy%26Wine&var3=%E2%9E%8B%E2%9E%8C%E2%9E%94
Rather than:
%3Fvar1%3DBruce+Oxford%26var2%3DBrandy%26Wine%26var3%3D%E2%9E%8B%E2%9E%8C%E2%9E%94
Which is of course just gibberish.

Categories