帖子标题不会进入网址

时间:2015-09-25 09:59:15

标签: javascript php wordpress api

好的,所以即时通讯使用yahoos finance api获取股票数据,但不是手动添加每个股票报价,我希望它使用帖子标题。所以例如我想获得apple inc的股票数据,所以我创建了一个名为aapl的帖子(这就是他们的股票报价)。

现在我这样认为它会起作用,但它不会返回任何数据。它就像这个帖子标题没有被插入到网址中。

$s = file_get_contents("http://finance.yahoo.com/d/quotes.csv?s=<?php the_title(); ?>&f=snl1d1t1cv");

下面是用来从雅虎获取数据的代码。

<?php
/**
 * Class to fetch stock data from Yahoo! Finance
 *
 */

class YahooStock {

/**
 * Array of stock code
 */
private $stocks = array();

/**
 * Parameters string to be fetched   
 */
private $format;

/**
 * Populate stock array with stock code
 *
 * @param string $stock Stock code of company    
 * @return void
 */
public function addStock($stock)
{
    $this->stocks[] = $stock;
}

/**
 * Populate parameters/format to be fetched
 *
 * @param string $param Parameters/Format to be fetched
 * @return void
 */
public function addFormat($format)
{
    $this->format = $format;
}

/**
 * Get Stock Data
 *
 * @return array
 */
public function getQuotes()
{        
    $result = array();      
    $format = $this->format;

    foreach ($this->stocks as $stock)
    {            
        /**
         * fetch data from Yahoo!
         * s = stock code
         * f = format
         * e = filetype
         */
        $s = file_get_contents("http://finance.yahoo.com/d/quotes.csv?s=<?php the_title(); ?>&f=snl1d1t1cv");

        /** 
         * convert the comma separated data into array
         */
        $data = explode( ',', $s);

        /** 
         * populate result array with stock code as key
         */
        $result[$stock] = $data;
    }
    return $result;
}
} 


$objYahooStock = new YahooStock;

/**
Add format/parameters to be fetched

s = Symbol
n = Name
l1 = Last Trade (Price Only)
d1 = Last Trade Date
t1 = Last Trade Time
c = Change and Percent Change
v = Volume
*/
$objYahooStock->addFormat("snl1d1t1cv"); 

/**
Add company stock code to be fetched


aapl = Apple    
*/
$objYahooStock->addStock("<?php the_title(); ?>");


/**
 * Printing out the data
 */
foreach( $objYahooStock->getQuotes() as $code => $stock)
{
?>
Code: <?php echo $stock[0]; ?> <br />
Name: <?php echo $stock[1]; ?> <br />
Last Trade Price: <?php echo $stock[2]; ?> <br />
Last Trade Date: <?php echo $stock[3]; ?> <br />
Last Trade Time: <?php echo $stock[4]; ?> <br />
Change and Percent Change: <?php echo $stock[5]; ?> <br />
Volume: <?php echo $stock[6]; ?> <br /><br />

<?php
}
?>

1 个答案:

答案 0 :(得分:0)

当然以下行不正确:

$s = file_get_contents("http://finance.yahoo.com/d/quotes.csv?s=<?php the_title(); ?>&f=snl1d1t1cv");

那应该是:

$s = file_get_contents("http://finance.yahoo.com/d/quotes.csv?s=".the_title()."&f=snl1d1t1cv");

但我看不到对该特定功能的引用 - 是否在其他地方声明了?

然后,在您的代码中稍后会出现另一行看起来错误的行:

$objYahooStock->addStock("<?php the_title(); ?>");

应该是:

$objYahooStock->addStock( the_title() );

我不确定这或多或少是你所追求的,但我不知道我猜到了the_title()函数调用是什么。为了简洁/清晰,我稍微重写了您的代码并删除了所有注释:

<?php
    class YahooStock{
        private $stocks;
        private $format;

        public function __construct(){
            $this->stocks=array();  
        }
        public function addStock( $stock=false ){
            if( $stock ) $this->stocks[]=$stock;
        }
        public function addFormat( $format=false ){
            if( $format ) $this->format = $format;
        }
        public function getQuotes(){
            $result = array();
            foreach( $this->stocks as $stock ){
                $s = file_get_contents( "http://finance.yahoo.com/d/quotes.csv?s=".$stock."&f=".$this->format );
                $result[$stock] = explode( ',', $s);
            }
            return $result;
        }
    }

    $oYahoo = new YahooStock;
    $oYahoo->addFormat("snl1d1t1cv"); 
    $oYahoo->addStock('msft');
    $oYahoo->addStock('amzn');
    $oYahoo->addStock('yhoo');
    $oYahoo->addStock('goog');


    foreach( $oYahoo->getQuotes() as $code => $stock ) {
        echo "<pre>";
        echo "Code:".$stock[0].PHP_EOL;
        echo "Name:".$stock[1].PHP_EOL;
        echo "Last Trade Price:".$stock[2].PHP_EOL;
        echo "Last Trade Time:".$stock[4].PHP_EOL;
        echo "Last Trade Date:".$stock[3].PHP_EOL;
        echo "Change and Percent Change:".$stock[5].PHP_EOL;
        echo "Volume:".$stock[6];

        echo "</pre>";
    }
    $oYahoo=null;   
?>

This outputs the following:
---------------------------

Code:"msft"
Name:"Microsoft Corporation"
Last Trade Price:43.91
Last Trade Time:"4:00pm"
Last Trade Date:"9/24/2015"
Change and Percent Change:"+0.04 - +0.09%"
Volume:27906497

Code:"amzn"
Name:"Amazon.com
Last Trade Price: Inc."
Last Trade Time:"9/24/2015"
Last Trade Date:533.75
Change and Percent Change:"4:00pm"
Volume:"-2.32 - -0.43%"

Code:"yhoo"
Name:"Yahoo! Inc."
Last Trade Price:29.34
Last Trade Time:"4:00pm"
Last Trade Date:"9/24/2015"
Change and Percent Change:"-0.40 - -1.34%"
Volume:19109920

Code:"goog"
Name:"Google Inc."
Last Trade Price:625.80
Last Trade Time:"4:00pm"
Last Trade Date:"9/24/2015"
Change and Percent Change:"+3.44 - +0.55%"
Volume:2242735