Getting Wordpress RSS feed without making a HTTP request - php

I'm trying to get my blog's RSS feed and manipulate it in PHP. Accord to the documentation, the XML feed for all Wordpress blogs can be downloaded at this address:
http://www.example.com/feed/atom/
I've written some simple code that works fine on a test server, but won't work on my hosted server:
$feedUrl = 'http://www.example.com/blog/feed/atom/';
$rawFeed = file_get_contents($feedUrl);
$feedXML = new SimpleXmlElement($rawFeed);
The reason for this is because my hosting provider prevents scripts making HTTP (port 80) connections back to the same server that they're running on.
How can I get access to the feed without needing to do a HTTP request to the same server?
I have tried accessing the URL directly (i.e. /home/example.com/blog/feed/atom), but nothing is found because it needs a proper request to generate the XML RSS feed. I've also tried a CURL request, but I got the same result.
It's a tricky problem! Thanks for any help!
Note: My solution needs to run on a non-WP page.

Some hosting providers might let you set up CRON jobs through their admin console, without having access to the command line. In a situation like that, you may be able to use a WP-CLI command to retrieve the output of the feeds, and save it to a file using something like "> filename.txt" at the end of the command.
See here: http://wp-cli.org/
And possibly here: http://wp-cli.org/commands/eval-file/

Related

Requesting file via PHP fails, but succeeds in Python

The URL in question : http://www.roblox.com/asset/?id=149996624
When accessed in a browser, it will correctly download a file (which is an XML document). I wanted to get the file in php, and simply display its contents on a page.
$contents = file_get_contents("http://www.roblox.com/asset/?id=149996624");
The above is what I've tried using (as far as I know, the page does not expect any headers). I get a 500 HTTP error. However, in Python, the following code works and I receive the file.
r = requests.get("http://www.roblox.com/asset/?id=147781188")
I'm confused as to what the distinction is between how these two requests are sent. I am almost 100% it is not a header problem. I've also tried the cURL library in PHP to no avail. Nothing I've tried in PHP seems to succeed with the URL (with any valid id parameter); but Python is able to bring success nonchalantly.
Any insight as to why this issue may be happening would be great.
EDIT : I have already tried copying Python's headers into my PHP request.
EDIT2 : It also appears that there are two requests happening upon navigating to the link.
Is this on a linux/mac host by chance? If so you could use ngrep to see the differences on the request themselves on the wire. Something like the following should work
ngrep -t '^(GET) ' 'src host 127.0.0.1 and tcp and dst port 80'
EDIT - The problem is that your server is responding with a 302 and the PHP library is not following it automatically. Cheers!

How to use PHP to retrieve Wowza HTTP Providers

Wowza Streaming Server provides HTTP Providers for providing information about current connections and server status. They use basic HTTP authentication in the model of http://username:password#url.com:8086/connectioncounts. This will output an xml view of what's happening on the server and how many streaming connections there are.
I'm admittedly a PHP newb but I've searched through a fair amount of Stackoverflow and Google. I've tried many of the examples and so far all that is returned is nothing when trying to programmatically access the data.
I've attempted to use cURL as well use the simplexml tools. phpinfo() tells me I have all of the necessary packages to remotes work with and process urls and remote xml files. I have tested the same code with other pages like my website and it works just fine albeit my homepage doesn't use basic authentication.
Similar questions I've tried:
PHP Get XML from Remote URL with HTTP Authentication
How can I read a remote XML file that uses digest authentication?
Typical output looks like this:
This XML file does not appear to have any style information associated with it. The document tree is shown below.
<WowzaMediaServer>
<ConnectionsCurrent>1</ConnectionsCurrent>
<ConnectionsTotal>2</ConnectionsTotal>
<ConnectionsTotalAccepted>2</ConnectionsTotalAccepted>
<ConnectionsTotalRejected>0</ConnectionsTotalRejected>
<MessagesInBytesRate>1324.0</MessagesInBytesRate>
<MessagesOutBytesRate>0.0</MessagesOutBytesRate>
<VHost>
<Name>_defaultVHost_</Name>
<TimeRunning>5883.723</TimeRunning>
<ConnectionsLimit>0</ConnectionsLimit>
<ConnectionsCurrent>1</ConnectionsCurrent>
<ConnectionsTotal>2</ConnectionsTotal>
<ConnectionsTotalAccepted>2</ConnectionsTotalAccepted>
<ConnectionsTotalRejected>0</ConnectionsTotalRejected>
<MessagesInBytesRate>1324.0</MessagesInBytesRate>
<MessagesOutBytesRate>0.0</MessagesOutBytesRate>
<Application>
<Name>live</Name>
<Status>loaded</Status>
<TimeRunning>5877.738</TimeRunning>
<ConnectionsCurrent>1</ConnectionsCurrent>
<ConnectionsTotal>2</ConnectionsTotal>
<ConnectionsTotalAccepted>2</ConnectionsTotalAccepted>
<ConnectionsTotalRejected>0</ConnectionsTotalRejected>
<MessagesInBytesRate>1324.0</MessagesInBytesRate>
<MessagesOutBytesRate>0.0</MessagesOutBytesRate>
<ApplicationInstance>
<Name>_definst_</Name>
<TimeRunning>5877.622</TimeRunning>
<ConnectionsCurrent>1</ConnectionsCurrent>
<ConnectionsTotal>2</ConnectionsTotal>
<ConnectionsTotalAccepted>2</ConnectionsTotalAccepted>
<ConnectionsTotalRejected>0</ConnectionsTotalRejected>
<MessagesInBytesRate>1324.0</MessagesInBytesRate>
<MessagesOutBytesRate>0.0</MessagesOutBytesRate>
<Stream>
<Name>mystream.mp4</Name>
<SessionsFlash>0</SessionsFlash>
<SessionsCupertino>0</SessionsCupertino>
<SessionsSanJose>0</SessionsSanJose>
<SessionsSmooth>0</SessionsSmooth>
<SessionsRTSP>0</SessionsRTSP>
<SessionsMPEGDash>0</SessionsMPEGDash>
<SessionsTotal>0</SessionsTotal>
</Stream>
</ApplicationInstance>
</Application>
</VHost>
</WowzaMediaServer>
The default HTTP Authentication for wowza media server is digest.
But you can change it to basic in VHost.xml in conf folder.
Just change admin-digest
to be admin-basic

can you use curl to post to a local file?

I tried using curl to post to a local file and it fails. Can it be done? my two management systems are on the same server and it seems unnecessary to have it traverse the entire internet system just to go to a file on the same hard drive.
Using localhost didn't do the trick.
I also tried to $_SERVER[DOCUMENT_ROOT].'/dir/to/file.php' using post data. It's for an API that is encrypted, so I'm not sure exactly how it works. It's for a billing system I have and I just realized that it sends data back (API).
It's simply post data and an XML response. I could write an html form tag and input fields and get the same result, but there isn't really anything else to know.
The main question is: Does curl have the ability to post to a local file or not?
it is post data. it's for an API that is encrypted so i'm not sure exactly how it works
Without further details nobody can answer then what you should do.
But if it's indeed a POST receival script on the local server, then you can send a POST request to it using the URL:
$url = "https://$_SERVER[SERVER_NAME]/path/to/api.php";
And then receive its output from the cURL call.
$data = curl($url)->post(1)->postdata(array("billing"=>1234345))
->returntransfer(1)->exec();
// (you would use the cumbersome curl_setopt() calls instead)
So you get a XML or JSON or whatever response.
If they're on the same drive, then use file operations instead:
file_put_contents('/path/to/the/file', $contents);
Using CURL should only be done if you absolutely NEED the http layer to get involved for some reason, or if you're dealing with a remote server. Using HTTP would also mean you need to have the 'target' script be able to handle a file upload plus whatever other data you need to send, and then that script would end up having to do file operations ANYWAYS, so in effect you've gone on a round-the-world flight just so you can move from your living room to the kitchen.
file://locafilespec.ext worked for me. I had 2 files in the same folder on a linux box, in a folder that is not served by my webserver, and I used the file:// wrapper to post to file://test.php and it worked great. it's not pretty, but it'll work for dev until I move it to it's final resting place.
Does curl have the ability to post to a local file or not?
To curl local file, you need to setup HTTP server as file:// won't work, so:
npm install http-server -g
Then run the HTTP server in the folder where is the file:
$ http-server
See: Using node.js as a simple web server.
Then test the curl request from the command-line to localhost like:
curl http://127.0.0.1:8081/file.html
Then you can do the same in PHP.

php - how to make a proxy for a web service

I'm trying to get around an access control origin error for a web service by building a local proxy, but I'm not sure how to do it. The web service looks like the attached file, and is accessed directly by using the following URL:
https://url.com/SparkService.asmx?op=InsertConsumer
How would I write something locally that carried out this URL's functionality?
I built a PHP file that will pull the web service URLs contents, but it doesn't seem to carry out the functionality of that web service:
<?php
$op = htmlspecialchars($_GET["op"]);
$proxyURL = 'https://url.com/SparkService.asmx?op=' . $op;
die( file_get_contents($proxyURL) );
?>
The image shows you have to use a POST, which you can't do with bare-bones file_get_contents - it defaults to using a GET query. You'll have to use CURL, or set up a stream to configure and perform a POST.
I am not sure about your intension of doing this.
you can use WSO2 ESB proxy[1] to send messages to the real service through a local end point.
Or else you can create your own service by using WSO2 AS[2] and deploying a sample POJO as a web service.
[1] http://wso2.org/project/esb/java/4.0.0/docs/samples/proxy_samples.html
[2] http://wso2.org/project/app-server/

PHP: Curl can't grab a text-only page on my own site

I'm attempting to use curl inside php to grab a page from my own web server. The page is pretty simple, just has some plain text output. However, it returns 'null'. I can successfully retrieve other pages on other domains and on my own server with it. I can see it in the browser just fine, and I can grab it with command line wget just fine, it's just that when I try to grab that one particular page with curl, it simply comes up null. We can't use file_get_contents because our host has it disabled.
Why in the world would this be different behavior be happening?
Found the issue. I was putting my url someplace that was not in curl_init(), and that place was truncating the query string. Once I moved it back to curl_init, it worked.
Try setting curl's user agent. Sometimes hosts will block "bots" by blocking things like wget or curl - but usually they do this just by examining the user agent.
You should check the output of curl_error() and also take a look at your logfiles for the http server.

Categories