icon Get the most out of Surmunity, read our tips here! Need an interesting blog to read? You've got to read the Surpass Blog! | Welcome! Please register to access all of our features.

» Surpass Web Hosting Forums » Discussions » PHP, MySQL » file_get_contents problem? (Amazon API + PHP)

PHP, MySQL General PHP questions. Or go to our PHPsuexec Forum >>

Reply
 
LinkBack Thread Tools Search this Thread Rate Thread
Old October 18th, 2008, 11:46 PM   #1 (permalink)
JMF
Registered User
Seasoned Poster
 
JMF's Avatar
 
Joined in Jun 2004
Lives in Tennessee, USA
Hosted on Pass56
43 posts
Gave thanks: 1
Thanked 2 times
file_get_contents problem? (Amazon API + PHP)

Hi,

I'm dipping my feet into some custom stuff with the Amazon store API, and have hit a snag. I'm messing with the following code to generate a list of ASINs related to a given product. That's all it does. Here's the code (it may be a bit inelegant...I'm just learning!)

PHP Code:
<?php
//Script to generate similar ASINs to current music review
//Reference: http://docs.amazonwebservices.com/AWSECommerceService/latest/GSG/
          
define("Access_Key_ID""[OMITTED FOR POST]");
          
define("Associate_tag""[OMITTED FOR POST]");


            function 
get_info($Keywords,$SearchIndex){
                
$Operation "ItemSearch";
                
$Version "2008-08-19";
                
$ResponseGroup "Similarities";

                
$request=
                    
"http://ecs.amazonaws.com/onca/xml"
                        
"?Service=AWSECommerceService"
                        
"&AssociateTag=" Associate_tag
                        
"&AWSAccessKeyId=" Access_Key_ID
                        
"&Operation=" $Operation
                        
"&Version=" $Version
                        
"&SearchIndex=" $SearchIndex
                        
"&Keywords=" $Keywords
                        
"&ResponseGroup=" $ResponseGroup;

                
//Catch the response in the $response object
                
$response file_get_contents($request);
                
$parsed_xml simplexml_load_string($response);
                
$widget_list make_list($parsed_xml);
                return 
$widget_list;
            }

function 
make_list($parsed_xml){
       
$i=0;
       foreach(
$parsed_xml->Items->Item->SimilarProducts->SimilarProduct as $current){
                    
$asin_results[$i] = $current->ASIN;
                    
$i++ ;
                }

       
$list_o_stuff implode(",",$asin_results);
       return 
$list_o_stuff;
            }

$widget_asins get_info("B000I2K9M4","Music");
echo 
$widget_asins;
            
?>
If I run this locally on my machine, I get the expected output: B0001KL526,B000H7JDZO,B000FS9LKW,B000H0MMKY,B000GU K0HM

(Sorry I can't reproduce it fully here without adding in my Amazon ID and key.)

When I put it in a web page on the server, though, it doesn't work. I can echo the $request string, but when I try to echo the $response, it comes up empty.

Could this have something to do with file_get_contents on my server? Or is it probably something else?

Thanks for any suggestions!
Jeff.
JMF is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old October 19th, 2008, 12:54 AM   #2 (permalink)
Surpass Fan
Super #1
 
fury's Avatar
 
Joined in Dec 2005
Lives in MN > USA
Hosted on pass84
2,140 posts
Gave thanks: 89
Thanked 112 times
Send a message via AIM to fury Send a message via Skype™ to fury
You could try using cURL instead.

PHP Code:
function curl_get_file_contents($request)
    {
        
$c curl_init();
        
curl_setopt($cCURLOPT_RETURNTRANSFER1);
        
curl_setopt($cCURLOPT_URL$request);
        
$response curl_exec($c);
        
curl_close($c);
    } 
Comment out (//) the current $response line and replace it with that code.
__________________
fury™ - not helping the situation since 1987
robmonroe.net | Twitter | Foursquare
fury is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old October 19th, 2008, 9:01 AM   #3 (permalink)
JMF
Registered User
Seasoned Poster
 
JMF's Avatar
 
Joined in Jun 2004
Lives in Tennessee, USA
Hosted on Pass56
43 posts
Gave thanks: 1
Thanked 2 times
That did the trick, thanks! (Do we know why?)
JMF is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old October 19th, 2008, 4:57 PM   #4 (permalink)
DemonicAngel
Super #1
 
twirp's Avatar
 
Joined in Aug 2004
Lives in Wherever The World Takes Me
Hosted on Pass76
1,942 posts
Gave thanks: 29
Thanked 40 times
Send a message via ICQ to twirp Send a message via AIM to twirp Send a message via MSN to twirp Send a message via Yahoo to twirp
Quote:
Originally Posted by JMF View Post
That did the trick, thanks! (Do we know why?)
allow_url_open is turned off by default for security reasons. You can enable it using a custom php.ini, but it's much safer to use cURL instead.
__________________
You wear Vans so high school kids will think that you can skate. He wears Vans because he can skate. TwiRp wears Vans because they were on sale. Pass76 wants Vans.
twirp is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old October 20th, 2008, 12:08 AM   #5 (permalink)
Surpass Fan
Super #1
 
fury's Avatar
 
Joined in Dec 2005
Lives in MN > USA
Hosted on pass84
2,140 posts
Gave thanks: 89
Thanked 112 times
Send a message via AIM to fury Send a message via Skype™ to fury
Glad I could help!
__________________
fury™ - not helping the situation since 1987
robmonroe.net | Twitter | Foursquare
fury is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On