2016-12-20 5 views
2

Ich habe den folgenden Code:Ausgabe mit fopen Ausgang

$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); 

, die gibt der folgende:

{"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} 

Mein Problem ist, kann ich nicht zur Ausgabe des Array zu bekommen scheinen, wo ich kann manipuliere es. Ich versuchte, extract() zu verwenden und foreach() zu verwenden, aber sein Handeln wie sein eine Schnur. Aber was mich ratlos ist, ist die Tatsache, dass ich nur die Ausgabe var_dump() kann. Wenn jemand weiß, was ich falsch mache, lass es mich wissen. Ich kann nur daran denken, ist, wenn die Ausgabe nur eine Zeichenfolge ist, gibt es eine Möglichkeit, es wieder in ein Array zu verwandeln?

+2

Ja, Sie können, verwenden Sie json_decode ($ json, true) es wird die json in ein Array umwandeln –

+0

ich 'echo json_decode ($ fp)' und es gibt mir ein Fehler von 'json_decode() erwartet Parameter 1 als String, Ressource gegeben ' –

+0

Und guter Haken Ich habe nicht darüber nachgedacht im json Format zu sein –

Antwort

2

Sie sollten den Inhalt aus der Datei lesen und dann

$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); 

Verweis auf JSon konvertieren: fread prüfen Beispiel # 3 Remote Fread() Beispiele

+0

das funktionierte, extrahierte und gab aus, was ich brauchte. Vielen Dank Sir, –

0

Bitte versuchen Sie folgenden Code:

$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); 

Dank ...

+0

$ fp ist eine Ressource, keine JSON-Zeichenfolge. – weirdo

+0

@weisdo, also wie würdest du vorschlagen, dies zu handhaben? –

1

versuchen Sie dies zu verwenden;

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

Wenn dies wie @bansi Antwort funktioniert es funktioniert. Danke –

+0

Versuchen Sie nie, Ihre URL zu verwenden. hoffe es funktioniert. aber andere Methode, um die Zeichenfolge aus dem Stream zu bekommen. tq – weirdo

0

Versuchen Sie, diese

 $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);