2014-09-18 4 views
-1

Je commence à apprendre le PHP en jouant avec l'API de wunderground. J'ai l'exemple de code ci-dessous d'eux qui fonctionne localement lors de tests avec xanampp/apache mais quand je le mets sur un serveur web réel en direct sur Internet, il ne semble pas faire l'appel au service Web.Demande de service Web en PHP

code:

<?php 
    $json_string = file_get_contents("http://api.wunderground.com/api/key/geolookup/conditions/q/IA/Cedar_Rapids.json"); 
    $parsed_json = json_decode($json_string); 
    $location = $parsed_json->{'location'}->{'city'}; 
    $temp_f = $parsed_json->{'current_observation'}->{'temp_f'}; 
    echo "Current temperature in ${location} is: ${temp_f}\n"; 


?> 

Résultats localement sur ma machine avec xampp:

Current temperature in Cedar Rapids is: 77.4 

Résultats sur serveur Web que je paie pour héberger des pages:

Current temperature in is: 

rapport d'erreur:

Warning: file_get_contents(): http:// wrapper is disabled in the server configuration by allow_url_fopen=0 in /hermes/bosnaweb01b/b2414/ipg.domainID/testt.php on line 5 Warning: file_get_contents(http://api.wunderground.com/api/key/geolookup/conditions/q/IA/Cedar_Rapids.json): failed to open stream: no suitable wrapper could be found in /hermes/bosnaweb01b/b2414/ipg.domainID/testt.php on line 5 Notice: Trying to get property of non-object in /hermes/bosnaweb01b/b2414/ipg.domainID/testt.php on line 7 Notice: Trying to get property of non-object in /hermes/bosnaweb01b/b2414/ipg.domainID/testt.php on line 7 Notice: Trying to get property of non-object in /hermes/bosnaweb01b/b2414/ipg.domainID/testt.php on line 8 Notice: Trying to get property of non-object in /hermes/bosnaweb01b/b2414/ipg.domainID/testt.php on line 8 Current temperatdure in is: 
+0

quoi faire: 'echo" La température actuelle dans ". $ Location." Est: ". $ temp_f. "\ n"; – Pogrindis

+1

Avez-vous activé le rapport d'erreurs complet? La raison la plus probable est que votre fichier json ne peut pas être récupéré (soit parce que 'allow_url_fopen' est désactivé, soit parce qu'il y a d'autres problèmes.) – Wrikken

+0

@Wrikken ne sais pas comment activer le rapport d'erreurs. avoir une certaine expérience avec les services Web, mais pas avec php – user2941841

Répondre

0

Réponse: Les serveurs Web n'autorisent pas souvent file_get_contents. En utilisant curl:

$curl = curl_init(); 
// Set some options - we are passing in a useragent too here 
curl_setopt_array($curl, array(
    CURLOPT_RETURNTRANSFER => 1, 
    CURLOPT_URL => "http://api.wunderground.com/api/key/geolookup/conditions/q/IA/Cedar_Rapids.json", 
    CURLOPT_USERAGENT => 'Codular Sample cURL Request' 
)); 
// Send the request & save response to $resp 
$resp = curl_exec($curl); 
// Close request to clear up some resources 
curl_close($curl); 

$json_string = $resp; 
$parsed_json = json_decode($json_string); 
    $location = $parsed_json->{'location'}->{'city'}; 
    $temp_f = $parsed_json->{'current_observation'}->{'temp_f'}; 
    echo "Current temperatdure in ${location} is: ${temp_f}\n"; 
+0

Ceci est une alternative, pas une "réponse" à la question. Il pourrait même être tout aussi probable que les serveurs Web n'autorisent pas cURL souvent. –

+0

@scrowler Mais si c'est le seul à faire ce que je veux dans mon environnement et il n'y a aucun moyen de le faire en utilisant file_get_contents sans les changements de serveur à php.ini cela signifie-t-il que la question ne répond jamais? – user2941841