Read specific remote line with PHP - php

I'm trying to find a way to load my latest tweet with PHP.
At this URL, I change "Twitter.xml" to my username-".xml."
https://twitter.com/users/show/Twitter.xml
The latest tweet is shown on line 49, but I don't think PHP can read a remote file... I want it to show line 49, but how do I combine it with finding a file on a remote server?
Thanks

In PHP you can use file_get_contents() to get the contents of that url. For example
$xml = file_get_contents('https://twitter.com/users/show/Twitter.xml');
This will only work if the config setting for allow_url_fopen is set to On

Use curl to fetch a remote resource.
http://www.php.net/manual/en/curl.examples-basic.php
http://www.php.net/manual/en/function.curl-init.php
Then, since it's xml, I would convert it to a native PHP object, using SimpleXML
http://www.php.net/manual/en/simplexml.examples-basic.php

Related

file_get_contents not working when parameters included in a relative url

I am trying to create a simple web service that will give a result depending on parameters passed.
I would like to use file_get_contents but am having difficulties getting it to work. I have researched many of the other questions relating to the file_get_contents issues but none have been exactly the situation I seem to having.
I have a webpage:
example.com/xdirectory/index.php
I am attempting to get the value of the output of that page using:
file_get_contents(urlencode('https://www.example.com/xdirectory/index.php'));*
That does not work due to some issue with the https. Since the requesting page and the target are both on the same server I try again with a relative path:
file_get_contents(urlencode('../xdirectory/index.php'));
That does work and retrieves the html output of the page as expected.
Now if I try:
file_get_contents(urlencode('../xdirectory/index.php?id=100'));
The html output is (should be): Hello World.
The result retrieved by the command is blank. I check the error log and have an error:
[Fri Dec 04 12:22:54 2015] [error] [client 10.50.0.12] PHP Warning: file_get_contents(../xdirectory/index.php?id=100): failed to open stream: No such file or directory in /var/www/html/inventory/index.php on line 40, referer: https://www.example.com/inventory/index.php
The php.ini has these set:
allow_url_fopen, On local and On master
allow_url_include, On local and On master
Since I can get the content properly using only the url and NOT when using it with parameters I'm guessing that there is an issue with parameters and file_get_contents. I cannot find any notice against using parameters in the documentation so am at a loss and asking for your help.
Additional Notes:
I have tried this using urlencode and not using urlencode. Also, I am not trying to retrieve a file but dynamically created html output depending on parameters passed (just as much of the html output at index.php is dynamically created).
** There are several folks giving me all kind of good suggestions and it has been suggested that I must use the full blown absolute path. I just completed an experiment using file_get_contents to get http://www.duckduckgo.com, that worked, and then with a urlencoded parameter (http://www.duckduckgo.com/?q=php+is+cool)... that worked too.
It was when I tried the secure side of things, https://www.duckduckgo.com that it failed, and, with the same error message in the log as I have been receiving with my other queries.
So, now I have a refined question and I may need to update the question title to reflect it.
Does anyone know how to get a parameterized relative url to work with file_get_contents? (i.e. 'file_get_contents(urlencode('../xdirectory/index.php?id=' . urlencode('100'))); )
Unless you provide a full-blown absolute protocol://host/path-type url to file_get_contents, it WILL assume you're dealing with a local filesystem path.
That means your urlencode() version is wrongly doing
file_get_contents('..%2Fxdirectory%2Findex.php');
and you are HIGHLY unlikely to have a hidden file named ..%2Fetc....
call url with domain, try this
file_get_contents('https://www.example.com/inventory/index.php?id=100');
From reading your comments and additional notes, I think you don't want file_get_contents but you want include.
see How to execute and get content of a .php file in a variable?
Several of these answers give you useful pointers on what it looks like you're trying to achieve.
file_get_contents will return the contents of a file rather than the output of a file, unless it's a URL, but as you seem to have other issues with passing the URI absolutely....
So; you can construct something like:
$_GET['id'] = 100;
//this will pass the variable into the index.php file to use as if it was
// a GET value passed in the URI.
$output = include $_SERVER['DOCUMENT_ROOT']."/file/address/index.php";
unset($_GET['id']);
//$output holds the HTML code as a string,
The above feels hacky trying to incorporate $_GET values into the index.php page, but if you can edit the index.php page you can use plain PHP passed values and also get the output returned with a specific return $output; statement at the end of the included file.
It has been two years since I used PHP so I am just speculating about what I might try in your situation.
Instead of trying fetching the parsed file contents with arguments as a query string, I might try to set the variables directly within the php script and then include it (that is if the framework you use allows this).
To achive this I would use pattern:
ob_start -> set the variable, include the file that uses the variable -> ob_get_contents -> ob_end_clean
It is like opening your terminal and running the php file with arguments.
Anyway, I would not be surprised if there are better ways to achieve the same results. Happy hacking :o)
EDIT:
I like to emphasize that I am just speculating. I don't know if there are any security issues with this approach. You could of course ask and see if anyone knows here on stackoverflow.
EDIT2:
Hmm, scrap what I said last. I would check if you can use argv instead.
'argv' Array of arguments passed to the script. When the script is run on the command line, this gives C-style access to the command line parameters. When called via the GET method, this will contain the query string. http://php.net/manual/en/reserved.variables.server.php
Then you just call your php script locally but without the query mark indicator "?". This way you can use the php interpreter without the server.
This is likely to be the most general solution because you can also use argv for get requests if I am understanding the manual correctly.

PHP XML http response in an array

I am a newbie in PHP. I am making a call to an http api using wget (the hosting site doesn't offer http_get). The call returns an xml set when do it manually. But it appears that my wget call is putting the response into an array.
I am unable to access the array and not sure where to go from here.
Below is my code. FYI -- The first 4 elements of the xml play_by_play object are: id, visitor, home, status.
<?php
exec('wget http://api.sportsdatallc.org/mlb-t3/pbp/99a0f209-2c69-49a4-99f9-8aebdf55b6e9.xml?api_key=API_KEY', $array);
//print_r(array_values($array));
echo $array["play_by_play"][0]->id;
?>
I appreciate any assistance with this!
Thanks
When you use exec(), the 2nd parameter ($array) will be filled with each line of the output. That's not what you want. That's why you can't access your values, it's just an aray of strings, not a parsed XML file.
I suggest you use shell_exec() instead.
$xml = shell_exec('wget http://your.xml.file');
Then you can use an XML parser on $xml.
I don't know why you're using exec here when PHP has file downloading built-in. http_get() is not a part of the PHP core, that's why your hosting doesn't offer it. If they let you use exec(), then I'll assume they'll let you use the built-in file_get_contents().
$xml = file_get_contents('http://your.xml.file');

How is this valid xml?

I'm calling a webservice that returns an XML document. When I call it it returns the following:
resultsetrecordtxnref1013101155943920/txnrefchannelvisa/channelamount1000.00/amountpayment_date4/11/2013 3:59:43 PM/payment_datepayment_statussuccessful/payment_statusfield_valuesfield_valuesfieldnamesTest Test/namesacct_descFalse/acct_descacct_desc_order0/acct_desc_orderhiddenFalse/hiddendefaultvalue /xpath_field0/xpath_field/fieldfieldamount1000.00/amountacct_descFalse/acct_descacct_desc_order0/acct_desc_orderhiddenFalse/hiddendefaultvalue /xpath_field0/xpath_field/fieldfieldcurrencyNGN/currencyacct_descFalse/acct_descacct_desc_order0/acct_desc_orderhiddenFalse/hiddendefaultvalue /xpath_field0/xpath_field/fieldfieldemail_addresstest/email_addressacct_descFalse/acct_descacct_desc_order0/acct_desc_orderhiddenFalse/hiddendefaultvalue /xpath_field0/xpath_field/fieldfieldphone_number+2348031155249/phone_numberacct_descFalse/acct_descacct_desc_order0/acct_desc_orderhiddenFalse/hiddendefaultvalue /xpath_field0/xpath_field/fieldfieldmerch_txnref0/merch_txnrefacct_descFalse/acct_descacct_desc_order0/acct_desc_orderhiddenFalse/hiddendefaultvalue /xpath_field0/xpath_field/field/field_values/field_valuespayment_status_descriptionTransaction Successful - Approved/payment_status_description/record/resultset
Is this valid XML because when I use simplexml_load_string it works on my local server which runs PHP 5.4 but not on my testing server which uses PHP 5.3
This is a result of your browser parsing the XML and ignoring all the tags. Try viewing the page source or using a browser that can handle XML.
For some reason setting turning on the php_openssl.dll fixed the problem. Not sure why ... but google says so

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?

PHP Error: Warning: session_start() [function.session-start]: Node no longer exists

Getting the following error when trying to start a session:
Warning: session_start() [function.session-start]: Node no longer exists in file.php on line 3
The script uses SimpleXML to parse XML files from remote hosts. It's running on a Linux Ubuntu server with PHP 5.2.6.
Has anyone come across this message before or have an insight in to what it means?
See explanation at the bottom of this page
[2009-09-25 11:41 UTC] rrichards#php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php
Cannot serialize object wrapping 3rd party library structs. Must
serialize the xml (to a string) and store that to session and reload the
xml when restoring from session
Start here:
http://bytes.com/topic/php/answers/831550-session_start-node-no-longer-exists
It looks like the variable you're working with isn't an array or variable in the traditional sense: it acts more like a resource. You're going to have to loop out the values like you might with a MySQL $result.
You can't store SimpleXML results in a session. Convert it to an array or extend it with wake and sleep magic methods.
I had the same issue and got a fix from the site http://www.ossramblings.com/simple_xml_breaks_sessions
Actually the session will not work well while trying to store XML data, so just convert the XML data (I mean objects) to a string as below:
$temp_max_markers = (string)$Response->owner->max_markers;
$_SESSION['max_markers'] = $temp_max_markers;
also u cannot use the xml in session name as .. $_SESSION[xml];
You can change the encryption key in config file in application folder in codeigniter it works for me..

Categories