Decoding URL in PHP - php

I am passing url variables, and this URL variables has been encoded. My goal was to decode the URL that has been passed and use this URL to get its DOM. The code below is what I am using but failed to accomplish my goal. It just shows a blank page. But if I decode the url first manually and try it, it works.
NOTE, the URL has an extention of MP4 but it is in html format. So don't let it confuse you.
$url_link = urldecode('http%3A%2F%2Fplaybb.me%2Fembed.php%3Fw%3D600%26%23038%3Bh%3D438%26%23038%3Bvid%3Dat%2Fnw%2Fzettai_shougeki_platonic_heart_-_01.mp4');
$html = file_get_html($url_link);
$raw_html = $html->find('html', 0)->innertext;
echo $raw_html;

Related

Parsing Wikipedia API with PHP

I have a PHP script that retrieves the JSON result from the Wikipedia API and stores it in $json variable, then I json decode it into $data:
<?php
$q = htmlspecialchars(($_GET["q"]));
$url = 'https://en.wikipedia.org/w/api.php?action=query&list=search&srnamespace=0&srprop=snippet&format=json&callback=json&origin=*&prop=links|extracts|categories|images&srsearch=test';
$json = file_get_contents($url);
/*
print "<pre>";print_r($json);print "</pre>";
*/
$data = json_decode($json,true);
echo $data['query']['search'][0]['title'];
This retrieves the JSON file, but I am not able to work with it. I need to extract the Title tag and echoing it like this doesn't do anything.
echo $data['query']['search'][0]['title'];
Any idea how to correct my code to retrieve the following title tag:
Remove &callback=json from your URL. That's making a request for JSONP (ironic link to wikipedia). It wraps the response with a JavaScript callback function, so instead of just JSON you need in PHP, you're getting
/**/json(THE JSON HERE)
You can see it in the page source, even if it displays as JSON on the page. Those extra characters are making json_decode fail. That parameter is intended more for cross-domain requests from JS.
It looks like you're already accessing the resulting array properly with
echo $data['query']['search'][0]['title'];
You might think it would give you some kind of warning or notice when you try to access those array keys when $data is null, but it won't.

Encoding and decoding in PHP doesn't work

In my html file I am encoding an URL like this:
encodeURIComponent(url);
In my PHP file I use GET to grab the url and sanitize:
$url = filter_var(filter_var(($_GET['url']), FILTER_SANITIZE_URL),FILTER_SANITIZE_STRING);
I'm experiencing some trouble when adding other parameters to the URL. When I take out the javascript encodeURIComponent the whole thing starts working but I really would like to use encodeURIComponent, so I tried to decode the URL on PHP side:
$url = filter_var(filter_var((urldecode($_GET['url'])), FILTER_SANITIZE_URL),FILTER_SANITIZE_STRING);
Doesn't work, I tried other things with no luck. I don't see how the above wouldn't work. What else can I try, what am I doing wrong?

Retrieve content of another website

I want to get the content of another page. The background is that I wanted to make an AJAX request but due to the Same Origin Policy I cannot do this. Now I wanted to write an own PHP script on which I make the AJAX request. The URL looks like the following:
http://domain.com/subfolder/another_subfolder/index.php?id=1234&tx_manager_pi9[parameter]=1&tx_manager_pi9[category]=test&tx_manager_pi9[action]=getInfos&tx_manager_pi9[controller]=Finder&cHash=123456789001233455332
I tried it with fopen, curl and file_get_contents. Nothing from the works. The problem is if I put in the URL as string like
$results = file_get_contents('http://domain.com/subfolder/another_subfolder/index.php?id=1234&tx_manager_pi9[parameter]=1&tx_manager_pi9[category]=test&tx_manager_pi9[action]=getInfos&tx_manager_pi9[controller]=Finder&cHash=123456789001233455332');
it does work. If I put in a variable
$url = 'http://domain.com/subfolder/another_subfolder/index.php?id=1234&tx_manager_pi9[parameter]=1&tx_manager_pi9[category]=test&tx_manager_pi9[action]=getInfos&tx_manager_pi9[controller]=Finder&cHash=123456789001233455332';
$results = file_get_contents($url);
I come to a wrong page. With the specific parameter I get a result. If the parameter are not given correctly it seems that I come to a default page. I can't make a sense out of it.
The same for curl:
$curlSession = curl_init();
$options = array
(
CURLOPT_URL=>$url,
CURLOPT_HEADER=>false,
CURLOPT_RETURNTRANSFER=>true,
CURLOPT_FOLLOWLOCATION=>true
);
curl_setopt_array($curlSession,$options);
$results = curl_exec($curlSession);
This doesn't work. If I put in the URL as string and not as variable I get some results! I thought that the ampersand & or the square brackets [] are the problem but I cannot say this. & should be reserved and [] are no correct URL parameters. But why does the direct input work and not the variable?
I used the variable because I make some replacements with str_replace where I make the query more flexible.
I saw similar questions here (cURL function not working, curl_setopt doesnt work with url as a variable) but there was never posted a real solution.
You have a , instead of a ; in your second code block.
Are you required to be "logged in" to the site that you're visiting? That would explain why it's working in your browser and not through your server script.
If all else is the same, your browser and the PHP functions you listed should return the same results.
Could you provide the actual URL for us to test?
EDIT: Based on the URL you provided, it's working fine for me:
php > $test = file_get_contents("http://www.domain.com/user/user_neu/index.php?id=16518&tx_stusermanager_pi9%5Bindications%5D=1&tx_stusermanager_pi9%5Bcategory%5D=cure&tx_stusermanager_pi9%5Baction%5D=getHousesByIndications&tx_stusermanager_pi9%5Bcontroller%5D=HouseFinder&cHash=88230660f01ads34d73a199b82e976");
php > var_dump($test);
string(29) "16,15,14,13,12,11,17,19,22"
My problem was that I used an encoded URL as starting point. E.g.
http://domain.com/subfolder/another_subfolder/index.php?id=1234&tx_manager_pi9%5Bparameter%5D=%23%23%23param1%23%23%23&tx_manager_pi9%5Bcategory%5D=%23%23%23param2%23%23%23&tx_manager_pi9%5Baction%5D=getInfos&tx_manager_pi9%5Bcontroller%5D=Finder&cHash=123456789001233455332
I made a str_replace on a URL encoded string. Even using urldecode afterwards the URL was not correctly generated for curl, file_get_contents, ...
The correct URL should be something like this
http://domain.com/subfolder/another_subfolder/index.php?id=1234&tx_manager_pi9[parameter]=###param1###&tx_manager_pi9[category]=###param2###&&tx_manager_pi9[action]=getInfos&tx_manager_pi9[controller]=Finder&&cHash=123456789001233455332
i.e. without &, %23, %5B, %5D

Get all content with file_get_contents()

I'm trying to retrieve an webpage that has XML data using file_get_contents().
$get_url_report = 'https://...'; // GET URL
$str = file_get_contents($get_url_report);
The problem is that file_get_contents gets only the secure content of the page and returns only some strings without the XML. In Windows IE, if I type in $get_url_report, it would warn it if I want to display everything. If I click yes, then it shows me the XML, which is what I want to store in $str. Any ideas on how to retrieve the XML data into a string from the webpage $get_url_report?
You should already be getting the pure XML if the URL is correct. If you're having trouble, perhaps the URL is expecting you to be logged in or something similar. Use a var_dump($str) and then view source on that page to see what you get back.
Either way, there is no magic way to get any linked content from the XML. All you would get is the XML itself and would need further PHP code to process and get any links/images/data from it.
Verify if openssl is enable on your php, a good exemple of how to do it:
How to get file_get_contents() to work with HTTPS?

Passing variables through GET for a file_get_contents - a different way

I have information stored in a session. But was I just found out, sessions don't work when you do file_get_contents().
So after some searching I thought of using this technic:
$cart = base64_encode(serialize($_SESSION['cart']));
I pass my session cart, serialize it and encode it. I then pass it into the file_get_contents.
$url = "http://www.domain.com/pdf_order.php?cart=".$cart;
$html = file_get_contents($url);
In the URL that it gets, I have this:
$cart = unserialize(base64_decode($_GET['cart']));
But I don't get anything.
I can print out the GET cart and I have an encoded string, but then I can't do anything with it.
Any help, much appreciated.
UPDATE
When I echo the URL I get this
http://www.domain.com/pdf_order.php?cart=YTozOntzOjc6Imdhcm1lbnQiO2E6MTp7aTowO2E6Nzp7czo0OiJ0eXBlIjtzOjU6IlNoaXJ0IjtzOjY6ImFjY2VzcyI7czozOiJ5ZXMiO3M6MzoiaWREIjtpOjEwNDtzOjY6ImZhYnJpYyI7YToyOntzOjQ6InR5cGUiO2E6Mzp7czo4OiJkYl92YWx1ZSI7czo0OiJiZXN0IjtzOjExOiJwcmV0dHlfbmFtZSI7czoxNToiSSB3YW50IHRoZSBiZXN0IjtzOjM6InR4dCI7czoxNDY6IjEwMCUgQ290dG9uPGJyIC8+SWYgeW91IHdhbnQgdGhlIGJlc3QsIHdlJ2xsIGdpdmUgdGhlIGJlc3QuICBDb3R0b24gaXMgdGhlIG1vc3QgY29tZm9ydGFibGUgZmFicmljIGFuZCBpcyBhIGZhYnJpYyB0aGF0IGJyZWF0aHMuLi55b3UnbGwgZW5qb3kgaXQuIjt9czo1OiJwcmljZSI7aTo5MDt9czozOiJpZFMiO3M6MjoiNTIiO3M6OToicXR5X1NoaXJ0IjtzOjE6IjQiO3M6NDoicXR5XyI7czoxOiI0Ijt9fXM6MzoiaWRBIjtzOjg6IjExMTExMTUxIjtzOjExOiJhY2Nlc3NvcmllcyI7YTo0OntpOjA7YTo0OntzOjM6ImlkQSI7czoxOiIxIjtzOjQ6Im5hbWUiO3M6ODoiU2xpbSB0aWUiO3M6MzoicXR5IjtzOjE6IjQiO3M6NToicHJpY2UiO3M6NToiMjcuOTkiO31pOjE7YTo0OntzOjM6ImlkQSI7czoxOiIyIjtzOjQ6Im5hbWUiO3M6ODoiU2xpbSB0aWUiO3M6MzoicXR5IjtpOjE7czo1OiJwcmljZSI7czo1OiIyNy45OSI7fWk6MjthOjQ6e3M6MzoiaWRBIjtzOjE6IjMiO3M6NDoibmFtZSI7czo4OiJTbGltIHRpZSI7czozOiJxdHkiO2k6MTtzOjU6InByaWNlIjtzOjU6IjI3Ljk5Ijt9aTozO2E6NDp7czozOiJpZEEiO3M6MToiNCI7czo0OiJuYW1lIjtzOjg6IlNsaW0gdGllIjtzOjM6InF0eSI7aToxO3M6NToicHJpY2UiO3M6NToiMjcuOTkiO319fQ==
Check out How to post data in PHP using file_get_contents? it's pretty much doing what you want to do, by creating a stream.
I'm guessing you can still put $_SESSION variables in there too.
If you are working on same server as www.domain.com then why do you need to use file_get_contents() to get some data or do some processing . Use direct calls.

Categories