2016-12-20 44 views
2

我有以下代码:问题与输出的fopen

$opts = array(
    'http'=>array(
    'method'=>"GET", 
    'header'=> "x-api-key: hidden" 
) 
); 
$context = stream_context_create($opts); 
$fp = fopen('https://mercury.postlight.com/parser?url=https://www.dev-metal.com/architecture-stackoverflow/', 'r', false, $context); 

fpassthru($fp); 
fclose($fp); 

其输出以下:

{"title":"The architecture of StackOverflow","author":null,"date_published":"2014-01-11T11:54:47.000Z","dek":null,"lead_image_url":"https://www.dev-metal.com/wp-content/uploads/2014/01/stackoverflow3.jpg","content":" 
One of the most interesting talks these weeks, and a rare insight into one of the most active pages on the web: Marco Cecconi of StackOverflow speaks about the general server architecture, why they don’t unit-test (!), how they release (5 times a day) and shows some awesome server load screenshots. It’s fascinating that they run one of the most trafficked pages (that also uses long-polling “real-time” messaging !) on just 25 servers, most of them on 10% load all the time. “We could run it on just 5 servers if needed”. Awesome. Nice statements regarding caching and using existing code, too. 
I really like the Get-Things-Done attitude and the simple, but productive view on workflow (use multiple monitors, don’t be the nerd sitting in front of a laptop). The code is not perfect (lots of static methods), they don’t even test, only have a hand full of developers (!) and nearly no downtime. Ah yes, and they run one of the most successful sites in the history of the internet. 
“Languages are just tools”. “You’ll be successful anyways, or fail anyways [it does not depend on the language].” I really like that guy. And by the way, they mainly use dot.net for the site. Make sure you also check out the links, especially #5 shows the current tech stack used in the company. 
And by the way, have you noticed that EXTREMELY huge presentation screen ? Awesome! They obviously did this in a cinema or university audimax. 
Update #1: The slides of this talk: 
https://speakerdeck.com/sklivvz/the-architecture-of-stackoverflow-developer-conference-2013 
","next_page_url":null,"url":"https://www.dev-metal.com/architecture-stackoverflow/","domain":"www.dev-metal.com","excerpt":"One of the most interesting talks these weeks, and a rare insight into one of the most active pages on the web: Marco Cecconi of StackOverflow speaks about the general server architecture, why they…","word_count":256,"direction":"ltr","total_pages":1,"rendered_pages":1} 

我的问题是,我似乎无法给阵列来输出去的地方,我可以操纵它。我尝试使用extract()并使用foreach()来输出,但它的行为就像它的一个字符串。但是我难过的是,我只能输出var_dump()这个事实。如果有人知道我做错了什么,请告诉我。只有我能想到的解决方法是,如果输出只是一个字符串,是否有办法将它变回数组?

+2

是的,你可以使用json_decode($ json的,真正的),它会在JSON转换为数组 –

+0

我做'回声json_decode($ FP)',它给了我'json_decode()的错误期望参数1是字符串,资源给定' –

+0

和好的捕获我没有想到它是在JSON格式 –

回答

2

你应该从文件中读取内容,然后转换成JSON

$fp = fopen('https://mercury.postlight.com/parser?url=https://www.dev-metal.com/architecture-stackoverflow/', 'r', false, $context); 
$contents = stream_get_contents($fp); 
$output = json_decode($contents, true); 
print_r($output); 

参考:fread检查实例# 3远程fread()示例

+0

这工作,做'提取'并输出我所需要的。谢谢,先生, –

0

请尝试以下代码:

$opts = array(
'http'=>array(
'method'=>"GET", 
'header'=> "x-api-key: hidden" 
) 
); 
$context = stream_context_create($opts); 
$fp = fopen('https://mercury.postlight.com/parser?url=https://www.dev-metal.com/architecture-stackoverflow/', 'r', false, $context); 
$output = json_decode($fp, true); 
print_r($output); 
fclose($fp); 

谢谢...

+0

$ fp是一个资源而不是json字符串。 – weirdo

+0

@weirdo,那么你会如何处理这个问题呢? –

1

尝试使用此;

header('x-api-key: hidden'); 
$str = file_get_contents($url); 
$output = json_decode($str, true); 
+0

如果这是像@ bansi回答它的工作。谢谢 –

+0

永远不要尝试使用你的网址。希望它的工作。但从流中获取字符串的方法不同。 TQ – weirdo

0

试试这个

 $opts = array(
    'http'=>array(
    'method'=>"GET", 
    'header'=> "x-api-key: hidden" 
) 
); 
$context = stream_context_create($opts); 

$fp = fopen('https://mercury.postlight.com/parser?url=https://www.dev-metal.com/architecture-stackoverflow/','r',false,$context); 
$jsonData = stream_get_contents($fp); 
$arrayData = json_decode($jsonData,true); 
print_r($arrayData);