2011-07-23 138 views
0

在本地主機上(在Windows上使用Wamp)。我試圖用這個:爲什麼不是這個PHP代碼片段工作?

<? 
$xml = simplexml_load_file(‘http://stocklamp.tumblr.com/api/read/xml’); 
$posts = $xml->xpath(「/tumblr/posts/post[@type=’regular’]」); 
foreach($posts as $post) {?> 
<?echo $post[‘id’];?> 
<?echo $post[‘url-with-slug’];?>」> 
<?echo $post->{‘regular-title’};?> 
<?echo $post->{‘regular-body’};?> 
<?echo date(「jS D M, H:i」,strtotime($post[‘date’]));?> 
<?}?> 

嘗試後,所有我看到的是這個在我的網站:

xpath(「/tumblr/posts/post[@type=’regular’]」); foreach($posts as $post) { ?> 」> {‘regular-title’};?> {‘regular-body’};?> 

我發現這裏的片段:

http://stocklamp.tumblr.com/post/274675902/putting-your-tumblr-posts-on-your-websites-the-easy-way

編輯:固定。現在我越來越

Parse error: syntax error, unexpected ':' in C:\wamp\www..\index.php on line 52

,並在這條線:

$xml = simplexml_load_file(‘http://stocklamp.tumblr.com/api/read/xml’);

我不斷收到此錯誤:http://codepad.org/7f1IejIG

好。現在我已經修好了,但是如何通過標記獲取帖子?

更改'type = ...'不起作用。

$posts = $xml->xpath("/tumblr/posts/post[@type='file']"); 
+2

你們的服務器支持[PHP短標籤(HTTP: //php.net/manual/en/ini.core.php)?或者有實際的PHP支持? –

+0

這發生在本地主機上。 – kdevs3

+0

請參閱:http://codepad.org/fum4lWon如果這是由PHP解析,它會引發錯誤。你看到一個錯誤?如果沒有,你的頁面沒有被PHP處理(見Cyclone的答案)。 –

回答

0

最後一個問題是要解決的是,博客上提供的API XML URL不正確。以下作品:

<?php 
$xml = simplexml_load_file('http://stocklamp.tumblr.com/api/read/'); // No /xml 
$posts = $xml->xpath('/tumblr/posts/post[@type="regular"]'); 
foreach($posts as $post) {?> 
<?echo $post['id'];?> 
<?echo $post['url-with-slug'];?>」> 
<?echo $post->{'regular-title'};?> 
<?echo $post->{'regular-body'};?> 
<?echo date("jS D M, H:i",strtotime($post['date']));?> 
<?}?> 

http://jfcoder.com/test/tumblrtest.php

編輯

嘗試運行該代碼沒有標籤穿插:

<?php 
$xml = simplexml_load_file('http://stocklamp.tumblr.com/api/read/'); // No /xml 
$posts = $xml->xpath('/tumblr/posts/post[@type="regular"]'); 
foreach($posts as $post) { 
    echo $post['id']; 
    echo $post['url-with-slug']; 
    echo $post->{'regular-title'}; 
    echo $post->{'regular-body'}; 
    echo date("jS D M, H:i",strtotime($post['date'])); 
} 
?> 
+0

該死的它不工作。這裏http://pastie.org/2257287 – kdevs3

+0

這是你的標記/代碼的複製/粘貼:http://jfcoder.com/test/tumblrtest.php –

+0

我不斷收到解析錯誤:語法錯誤,意外的$結束C:\ wamp \ www \ jessblum-proj \ index.php 62行 – kdevs3

3

您的頁面未作爲PHP運行。如果您查看源代碼,您會看到整個PHP代碼在您的頁面上顯示爲純HTML。

顯示的位在$ xml->位之後,因爲它認爲打開的php標記和 - >是一個大的html標記。

是.php文件的擴展名。什麼是文件名?你把這些代碼放在哪裏?嘗試用<?php替換<?,有時Web服務器沒有啓用短標籤。

+1

此外,它使用短標籤 – Colum

+0

@Colum添加了 – Cyclone

+0

擴展名。哈哈哇。感到噓噓。現在我有另一個問題。 – kdevs3

2

您正在使用捲曲的引號。試試這個:

<? 
    $xml = simplexml_load_file('http://stocklamp.tumblr.com/api/read'); 
    $posts = $xml->xpath("/tumblr/posts/post[@type='regular']"); 
    foreach($posts as $post) { 
    echo $post['id']; 
    echo $post['url-with-slug']; 
    echo $post->{'regular-title'}; 
    echo $post->{'regular-body'}; 
    echo date("jS D M, H:i",strtotime($post['date'])); 
    } 
?> 
+0

捲曲引號與否,php代碼會引發語法錯誤。這個PHP根本就沒有被執行 – Cyclone

+0

它會拋出一個解析錯誤:http://codepad.org/fum4lWon –

+0

你複製在捲曲的引號:) http://codepad.org/NfjO7wYU - 鍵盤上的錯誤是因爲他們可能出於安全原因而阻止了simplexml_load_file。 – AlienWebguy

相關問題