Link to home
Start Free TrialLog in
Avatar of johnkreagan
johnkreagan

asked on

PHP News Feed Problem

Hi,
I am somewhat new to PHP, but would like to add a news feed to my site. I looked through several options before I found DayLife's free API. I used some sample scripts in their "Cookbook" and got a working page on my local apache test server. when I upload the document to my site to test it live, the Method Call is formed properly(if you click the link you can see the response from DayLife), but no headlines are rendered.

My local PHP version is 5.2.9 while my sever runs 5.2.4. Due to my hosting company's interface, I cannot upgrade my server's PHP install. I have attached the code and two screenshots below.

Thank you for any help you can provide!
<?php
 
# Configure the daylife api server url
	$daylife_server = "freeapi.daylife.com";
	$protocol = "phprest";
	$version = "4.2";
	$publicapi_access_url = "http://" . $daylife_server . "/" .  
$protocol . "/publicapi/" . $version . "/";
	$method='search_getRelatedArticles';
 
# Configure your api credentials
	$accesskey = "807388d830ba3f43955a92e7c56dcda2";
	$sharedsecret = "41451421acf17d778f3419d8000b7b29";
 
 
# specify your query
 
$query = 'Bankruptcy Law';
 
$url_encoded_query = urlencode($query);
 
# Build your method signature
 
# For search_X methods, the Core Input is the query term itself $signature = md5($accesskey . $sharedsecret . $query);
 
$sort = 'date';
 
#Draw from news in last 3 days
$end_time = date("U");
$start_time = $end_time - (3 * 86400);
 
$composed_url = $publicapi_access_url . $method . '?accesskey=' .  
$accesskey . '&signature=' . $signature . '&query=' .  
$url_encoded_query;
 
# kick off the call
$result = file_get_contents($composed_url);
 
$relatedArticlesResponse = unserialize($result);
 
$relatedArticles = $relatedArticlesResponse['response']['payload']
['article'];
 
?>
 
<html>
	<head>
		<title>
			DayPI headline demonstration
		</title>
	</head>
<body>
<p>The Method Call:<br>
 
<?php print "<a href=". "$composed_url" . ">" . "$composed_url" . "</ 
a>"?>
 
<h1>Recent headlines for <?php print "$query" ?></h1>
 
 
<ul>
<?php
 
foreach ($relatedArticles as $article) {
	$source = $article['source'];
	
	$articleListing = "<li><b><a href=" . $article['url'] . ">" ;
	$articleListing .= $article['headline'] . '</a></b>';
	$articleListing .= " " . $article['timestamp'] . ", ";
	$articleListing .= " from the <i><a href=" .$source['url'] . ">" .  
$source['name'] . "</a></i> " ;
	echo $articleListing;
}
 
?>
 
</body>
</html>

Open in new window

localmac.png
remoteserver.png
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

This seems like an odd RESTful API - I wonder why it does not return XML?  But if it is free, that may explain a lot about the underlying technology ;-)

Your code is pretty straightforward.  I can see nothing that would matter about PHP version 5.2.9 on your local machine versus a sever that runs 5.2.4.

Let's try this... After these lines:

# kick off the call
$result = file_get_contents($composed_url);
 

Add this line:
var_dump($result);

Also you might want to var_dump($relatedArticlesResponse); after you unserialize the data.

This will give you some insight into what is coming back.  Is there any chance your IP address matters to the call?
Avatar of johnkreagan
johnkreagan

ASKER

Hey Ray...

Thanks for the help!!

I wondered about the XML myself, but free and easy usually wins out in my book. Or at least I thought it would be easy

I added the code and tested both locally and remotely. Locally I get a long response from the API which lists the articles info. Remotely, I do not see any response at all.

Do you think this could be an authentication issue where the Daylife API is only letting me call it from one IP address? That just seems a bit odd and restrictive to me....
Yes, I agree it might seem odd, but I work with some clients who authenticate the IP address as well as the client ID and password, so I guess it is not too far beyond the pale.

If you want to send me the real credentials offlist, I will try testing from here.

Use email RPaseur at NationalPres org
SOLUTION
Avatar of hielo
hielo
Flag of Wallis and Futuna image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@heilo: Good point.
@johnkreagan: Look at a printout of phpinfo() and be sure you have file_get_contents enabled and you are not running in "safe mode" or something like that.  GoDaddy is notoriously bad about this sort of stuff.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi everybody,

Thanks a bunch for the quick and informative responses. I've done some checking with Daylife(they do not block API calls based on IP and will allow them across multiple servers).

So I wanted to check in file_get_includes was turned on. First, I tried a sample script from W3C which included example.com. Worked fine locally, but when uploaded nothing at all appeared, so I believe file_get_includes is my problem....

My next step was to check out my php.ini file and check some things. First, safe mode is off. Next I checked if fopen was on and it appears to be. So Im a bit stuck.... If I cant figure this out soon, I think the curl method is probably my best bet.

If anybody has any other suggestions for me please let me know! And thanks again to everybody for your help!
?? http://us2.php.net/manual-lookup.php?pattern=file_get_includes - not a PHP function.

Here is what I get back when I use your script above with the var_dump() I suggested:
string(111) "a:1:{s:8:"response";a:3:{s:7:"message";s:23:"Invalid API Credentials";s:4:"code";i:-2001;s:7:"payload";a:0:{}}}"
Ray

Whoops... Sorry about those mistakes...

I meant file_get_contents.... and I tested the same code with the var dump() you suggested and got the same response on my local server where it works, but on my live server all I get is "bool(false)" where the long response shows locally....

I apologize if my inexperience is making it more difficult to help, but thanks again

No problem - we are here to help.

See my comment above at # 24814390

Best, ~Ray
Hi, John - I tested it by copying your script to my server and it worked just fine.  Hmm....
Hi Ray,

Thanks for all the help. I suppose the process of elimination would lead me to the server's PHP configuration. I will contact my provider and/or try the curl method tonight.

I will update this and award points either way tomorrow.

Thanks again everybody!
Yeah, check phpinfo() output and see what you can find there.  Best of luck with it, ~Ray
Thanks again for all the help, especially you Ray.

After opening a support ticket with my(soon to be former) hosting provider Globat, they informed me the file_get_contents function was disabled due to security reasons.

I was able to apply the curl method pretty easily to the code I was originally working with. I will attach the code below just in case this helps anyone else.

Thanks again

<?php // RAY_curl_example.php
error_reporting(E_ALL);
 
function my_curl($url)
{
    $curl = curl_init();
 
// HEADERS FROM FIREFOX - APPEARS TO BE A BROWSER REFERRED BY GOOGLE
 
    $header[] = "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
    $header[] = "Cache-Control: max-age=0";
    $header[] = "Connection: keep-alive";
    $header[] = "Keep-Alive: 300";
    $header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
    $header[] = "Accept-Language: en-us,en;q=0.5";
    $header[] = "Pragma: "; // browsers keep this blank.
	
	
 
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.15) Gecko/20080623 Firefox/2.0.0.15');
    curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
    curl_setopt($curl, CURLOPT_REFERER, 'http://www.google.com');
    curl_setopt($curl, CURLOPT_ENCODING, 'gzip,deflate');
    curl_setopt($curl, CURLOPT_AUTOREFERER, true);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_TIMEOUT, 10); // GIVE UP AFTER TEN SECONDS
 
    if (!$html = curl_exec($curl))
    {
        return FALSE;
    }
 
    curl_close($curl);
    return $html;
}
 
 # Configure the daylife api server url 
	$daylife_server = "freeapi.daylife.com";
	$protocol = "phprest";
	$version = "4.2";
	$publicapi_access_url = "http://" . $daylife_server . "/" . $protocol . "/publicapi/" . $version . "/";
	$method='search_getRelatedArticles';
 
	# Configure your api credentials 
		$accesskey = "XXX";
		$sharedsecret = "XXX";
	
	
	# specify your query
	
	$query = 'Karl Fisher Titration';
	
	$url_encoded_query = urlencode($query);
	
	# Build your method signature
	
	# For search_X methods, the Core Input is the query term itself
	$signature = md5($accesskey . $sharedsecret . $query);
	
	$sort = 'date';
	
	#Draw from news in last 3 days
	$end_time = date("U");
	$start_time = $end_time - (3 * 86400);
	
	$composed_url = $publicapi_access_url . $method . '?accesskey=' . $accesskey . '&signature=' . $signature . '&query=' . $url_encoded_query;
	
	
// USAGE EXAMPLE
$url = $composed_url;
$result = my_curl($url);
if (!$result) die("NO $url");
 
$relatedArticlesResponse = unserialize($result);
 
$relatedArticles = $relatedArticlesResponse['response']['payload']['article'];
 
?>
 
<html>
	<head>
		<title>
			DayPI headline demonstration
		</title>
	</head>
<body>
<p>The Method Call:<br>
 
<?php print "<a href=". "$composed_url" . ">" . "$composed_url" . "</a>"?>
 
<h1>Recent headlines for <?php print "$query" ?></h1>
 
 
<ul>
<?php
 
foreach ($relatedArticles as $article) {
	$source = $article['source'];
	
	$articleListing = "<li><b><a href=" . $article['url'] . ">" ;
	$articleListing .= $article['headline'] . '</a></b>';
	$articleListing .= " " . $article['timestamp'] . ", ";
	$articleListing .= " from the <i><a href=" .$source['url'] . ">" . $source['name'] . "</a></i> " ;
	echo $articleListing;
}
 
?>
 
</body>
</html>

Open in new window

Glad to help.  I use and recommend ChiHost.com - they have REAL PEOPLE who understand web hosting and you can get them on the phone when you have questions.  I have also recently used HostGator - their tech support was pretty good, too.  There are not many things I need help with when I am building a web site, but on those occasions when I need help, it's great to know it is there.  Usually it is a configuration issue on the server, and not something I can fix, so I need a response ASAP.  ChiHost has always come through for me.  Speak with Nick Gilbert and tell him I sent you.

Many of my colleagues love DreamHost.  The main reason I do not use them is an historical artifact now - I run email lists for several clients and had a perfectly legit need to send thousands of email messages each week.  DreamHost had a throttle on the number of messages and they would not budge an inch.  So they were out of consideration.

I do not recommend GoDaddy or SiteGround because of my experiences with their tech support.  They repeatedly wasted my time.

Best regards, ~Ray
johnkreagan

there are a lot of volunteers here looking for help with their own technical problems. However, most of these volunteers cannot afford (or don't want) to pay the monthly membership.  So they answer others' questions in order to meet minimum point quota to maintain a free membership.

In situations like this, where more than one expert helps in solving your problem, you are expected to split the points.
The reason for your problem AND alternate solution was clearly stated on post ID:24814548. So splitting the points was the proper thing to do. Even Ray would agree to that.

I don't really need the points, so leave the grading as it is. However, in the spirit of fairness, do keep this in mind for future posts.

Regards
Helio --

First off, thank for your help with this issue. As I was working on the question over two days, I must have forgotten that you originally suggested the cURL method. Obviously, that was vital to resolving the issue.

I know you said not to change the grading, but fair is fair. Ive contacted community support to see if they can change it, because I cant see a way to do it with the basic interface.

So once again, I really do appreciate the help and I apologize for screwing that up.
@hielo: Agreed!
@John: No worries, mate.  Hielo should get credit for contributing to the solution.

Best to all, ~Ray
>>After opening a support ticket with my(soon to be former) hosting provider Globat, they informed me the file_get_contents function was disabled due to security reasons.

Hmmm, if not having file_get_contents is the only reason you want to leave, if I were you I would reconsider. I would leave if there was no way to obtain external resources, but since they disabled for "SECURITY" reasons, then that's a PLUS in my book. That tells me they actually have security in mind! In case you are wondering, no I have affiliations with them. I've never heard of them or gone to their site as matte of fact. Just given my opinion based on what you posted.

>>thank for your help with this issue.
You are welcome

>>...Ive contacted community support...
Again, not necessary, but thank you. Honestly, it really is not a big deal to me. The only reason I pointed it out was for the sake of those that really try to keep up their point quota.  Last year I was more active and I did see quite a few complaints from posters about this issue. Not bringing it up to your attention is like "encouraging bad behaviour" :)

Regards