dcsimg
www.webdeveloper.com
Results 1 to 4 of 4

Thread: Webservice is not working on production server.

  1. #1
    Join Date
    Jan 2010
    Posts
    2

    Webservice is not working on production server.

    Hi all,

    I am new to this forum but hope you all will help me.

    I am trying to call a web service to convert currency. The problem is this when I am calling this web service from my local server its working fine but once I put this php code on my web server. Its not working.

    I have cross check with my hosting provider. He is saying everything is working fine.

    Can anyone tell me what I need to do and solve this problem.

    Many Thanks in advance

  2. #2
    Join Date
    Nov 2008
    Posts
    2,477
    Seriously? You post no code, don't even mention the webservice by name, give no information on software and hardware involved, and you expect us to be able to tell you why it's not working?! We are programmers not psychics! Give us a bit more to go on please.

  3. #3
    Join Date
    Jan 2010
    Posts
    2
    @Mindzai

    Sorry for that, I was thinking there is some settings for web server.

    So here we go..

    I am trying to call http://www.webservicex.net/CurrencyConvertor.asmx web service to convert currency from INR to USD on my localhost. Here everything working fine.

    But Once I put it on my apache web server. Here it is not working.

    currency_class.php
    HTML Code:
    <?php
    class JOJO_Currency
    {
        function getCurrencies()
        {
            return array(
                'NZD'  => "New Zealand Dollar",
                'USD'  => "U.S. Dollar",
                'EUR'  => "Euro",
                'GBP'  => "British Pound",
                'INR'  => "Indian Rupee",
            );
        }
    
        function getRate($from, $to)
        {
            return false;
        }
    }
    
    
    class JOJO_Currency_webservicex extends JOJO_Currency
    {
        function getRate($from, $to, $snoopy = true)
        {
            /* Set the $snoopy option to true to avoid any fopen file security issues on some shared hosts */
    
            $url = sprintf("http://www.webservicex.net/CurrencyConvertor.asmx/ConversionRate?FromCurrency=%s&ToCurrency=%s",
                    $from,
                    $to);
    
            if ($snoopy) {
                require_once(_PLUGINDIR . "/jojo_convert_currency/external/Snoopy/Snoopy.class.php");
                $snoopy = new Snoopy;
                $snoopy->fetch($url);
                $res = trim(strip_tags($snoopy->results));
            } else {
                $res = trim(strip_tags(implode('', file($url))));
            }
            return $res;
        }
    }
    
    class JOJO_Currency_yahoo extends JOJO_Currency {
    
         function _currency_api_get_desc($currency)
         {
            foreach ($this->getCurrencies() as $key => $description) {
                if ($key == $currency) {
                    return $description;
                }
            }
            return FALSE;
         }
    
         function getRate($currency_from, $currency_to, $snoopy = true)
         {
             $amount = 1;
             $currency_array = array(
                 's'  => 'Currencies',
                 'l1' => 'Last',
                 'd1' => 'Date',
                 't1' => 'Time'
                 );
    
             $result = array();
             $result['status']  = FALSE;
             $result['message'] = '';
             $result['value']   = '';
             $result['date']    = '';
             $result['time']    = '';
    
             $from = strtoupper($currency_from);
             $to   = strtoupper($currency_to);
    
             $url = 'http://finance.yahoo.com/d/quotes.csv?e=.csv&f='. $this->_currency_api_get_fields($currency_array) .'&s='. $from . $to .'=X';
    
             // Validate the passed currency codes, to make sure they are valid
             if (FALSE == $this->_currency_api_get_desc($from)) {
                 $msg = "currency: Invalid currency_from=$from";
                 $result['message'] = $msg;
                 $result['status'] = FALSE;
             }
    
             if (FALSE == $this->_currency_api_get_desc($currency_to)) {
                 $msg = "currency: Invalid currency_to=$to";
                 return FALSE;
                 $result['message'] = $msg;
                 $result['status'] = FALSE;
             }
    
             if (!is_numeric($amount)) {
                 $msg = "currency: Invalid amount=$amount";
                 $result['message'] = $msg;
                 $result['status'] = FALSE;
             }
    
             $handle = @fopen($url, 'r');
             if ($handle) {
                 if ($record = fgets($handle, 4096)) {
                     fclose($handle);
                     $currency_data = explode(',', $record);
                     $rate = $currency_data[1];
                     $date = $currency_data[2];
                     $time = $currency_data[3];
    
                     // Calculate the result
                     $value = $amount * $rate;
    
                     // Log it
    
                     // Got what we need
                     $result['value']  = $value;
                     $result['date']   = $date;
                     $result['time']   = $time;
                     $result['status'] = TRUE;
                     $result['message']= 'success';
                 } else {
                     $msg = 'currency: fgets failed';
                     $result['status'] = FALSE;
                     $result['message'] = $msg;
                 }
             } else {
                 $msg = 'currency: cannot contact Yahoo Finance host';
                 $result['status'] = FALSE;
                 $result['message'] = $msg;
             }
    
             return $result['value'];
         }
    
    
         function _currency_api_get_fields($array) {
             $field_string = '';
             while (list($field, $header) = each($array)) {
                 $field_string .= $field;
             }
    
             return $field_string;
         }
    }
    ?>
    index.php

    HTML Code:
    <?php
    /************* Exchange Rate Calculator *******************/
    /*
    Released by AwesomePHP.com, under the GPL License, a
    copy of it should be attached to the zip file, or
    you can view it on http://AwesomePHP.com/gpl.txt
    */
    /************* Exchange Rate Calculator *******************/	
    
    // Include Class
    require_once('currency_class.php');
    
    // Get list of currencies
    $c = new JOJO_Currency_yahoo();
    $list = $c->getCurrencies();
    
    // Check any submitions
    	// Amount to convert
    	$amount = 2000;
    	// From
    	$from = 'INR';
    	// To
    	$to = 'USD';
    	// Get rate
    	$rate = $c->getRate($from,$to, true);
    	// Total price (to 2 decemial points)
    	echo $total = number_format(($rate*$amount),2);
    	//'hi';
    
    ?>

  4. #4
    Join Date
    Jan 2009
    Posts
    3,346
    What does the Snoopy->fetch() method look like?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles