2009-09-08 9 views
2

Comment est-ce que je décode l'alimentation complète autour de 10k article sans limite de mémoire de php? je l'ai trouvé un code qui rompent l'alimentation à la partie: http://pastebin.com/m43c3384fPHP: JSON décodage question

comment faire modifier le code pour travailler avec le flux ci-dessous:

GAF_update_projects_vertical_callback({"projects":{"count":2,"items":[{"id":502817,"name":"SEO","url":"http:\/\/www.getafreelancer.com\/affiliates\/ref.php?u=boon86&l=http%3A%2F%2Fwww.getafreelancer.com%2Fprojects%2FSEO%2FSEO.502817.html","start_unixtime":1252386582,"start_date":"Tue, 08 Sep 2009 01:09:42 -0400","end_unixtime":1252818582,"end_date":"Sun, 13 Sep 2009 01:09:42 -0400","buyer":{"url":"http:\/\/www.getafreelancer.com\/affiliates\/ref.php?u=boon86&l=http%3A%2F%2Fwww.getafreelancer.com%2Fusers%2F1094422.html","id":1094422,"username":"eyalbiber"},"state":"A","short_descr":"Hi, My name is Eyal. I'm looking for seo expert to work with him on long term relationship. I need...","options":{"featured":0,"nonpublic":0,"trial":0,"fulltime":0,"for_gold_members":0,"hidden_bids":0},"budget":{"min":30,"max":250},"jobs":["SEO"],"bid_stats":{"count":0,"avg":false}},{"id":503874,"name":"websubmissionarticles","url":"http:\/\/www.getafreelancer.com\/affiliates\/ref.php?u=boon86&l=http%3A%2F%2Fwww.getafreelancer.com%2Fprojects%2FCopywriting%2Fwebsubmissionarticles.html","start_unixtime":1252386528,"start_date":"Tue, 08 Sep 2009 01:08:48 -0400","end_unixtime":1252559328,"end_date":"Thu, 10 Sep 2009 01:08:48 -0400","buyer":{"url":"http:\/\/www.getafreelancer.com\/affiliates\/ref.php?u=boon86&l=http%3A%2F%2Fwww.getafreelancer.com%2Fusers%2F39628.html","id":39628,"username":"seexpert"},"state":"A","short_descr":"need some1 to rewrite articles by taking them from the net from various min 5 sources. he\/she can draft...","options":{"featured":0,"nonpublic":0,"trial":0,"fulltime":0,"for_gold_members":0,"hidden_bids":0},"budget":{"min":30,"max":250},"jobs":["Copywriting"],"bid_stats":{"count":0,"avg":false}}]}}) 

J'ai essayé ce code ci-dessous:

<?php 
require("JSON.php"); 

$mochi_url = "http://api.getafreelancer.com/Project/Search.json?aff=480992&callback=GAF_update_projects_vertical_callback&order=submitdate&count=50&iads=true&nonpublic=0&charset=UTF-8"; 
$feed = file_get_contents($mochi_url); 

$feed_start=strpos($feed,"[{"); 

$feed = substr($feed,$feed_start+1); 
$feed = substr($feed,0,strlen($feed)-2); 
$games = explode("{\"id\": \"",$feed); 
unset($feed); 
for($x=1;$x<=count($games);$x++){ 
    $games[$x] = "{\"name\": \"".$games[$x]; 
    $games[$x] = substr($games[$x],0,strlen($games[$x])-2); 
    $games_decode = json_decode($games[$x], true); 

    foreach($games_decode as $varname => $varvalue) { 
      $mochi[$varname] = $varvalue; 
    } 

       $name = $mochi['name']; 



        echo "$x - Name: {$mochi['name']} <br>"; 
       unset ($games[$x]); 
} 

?> 

il me renvoie

Avertissement: argument non valide fourni pour foreach() dans/home/studiox/public_html/getafreelancer/au to.php sur la ligne 18 1 - Nom:

une idée?

+0

quelqu'un sait? S'il vous plaît aider .. –

Répondre

0

Si vous êtes à court de mémoire, essayez de modifier le paramètre memory_limit dans php.ini à quelque chose de plus grand que la valeur par défaut (la valeur par défaut peut être de 8 Mo, mais je ne suis pas certain).

La page Increase PHP memory limit sur drupal.org contient des exemples d'autres façons d'augmenter la limite de mémoire.

+0

J'ai édité la réponse .. comment analyser le flux correctement? –

+0

Cela ressemble à un problème différent d'un problème de mémoire. Je ne connais pas assez PHP pour pouvoir aider avec ça, désolé. –

1

Tout d'abord, échapper à votre chaîne JSON. Comme ceci:

$json_string = "{\"projects\":{\"count\":2,\"items\":[{\"id\":502817,\"name\":\"SEO\",\"url\":\"http:\/\/www.getafreelancer.com\/affiliates\/ref.php?u=boon86&l=http%3A%2F%2Fwww.getafreelancer.com%2Fprojects%2FSEO%2FSEO.502817.html\",\"start_unixtime\":1252386582,\"start_date\":\"Tue, 08 Sep 2009 01:09:42 -0400\",\"end_unixtime\":1252818582,\"end_date\":\"Sun, 13 Sep 2009 01:09:42 -0400\",\"buyer\":{\"url\":\"http:\/\/www.getafreelancer.com\/affiliates\/ref.php?u=boon86&l=http%3A%2F%2Fwww.getafreelancer.com%2Fusers%2F1094422.html\",\"id\":1094422,\"username\":\"eyalbiber\"},\"state\":\"A\",\"short_descr\":\"Hi, My name is Eyal. I'm looking for seo expert to work with him on long term relationship. I need...\",\"options\":{\"featured\":0,\"nonpublic\":0,\"trial\":0,\"fulltime\":0,\"for_gold_members\":0,\"hidden_bids\":0},\"budget\":{\"min\":30,\"max\":250},\"jobs\":[\"SEO\"],\"bid_stats\":{\"count\":0,\"avg\":false}},{\"id\":503874,\"name\":\"websubmissionarticles\",\"url\":\"http:\/\/www.getafreelancer.com\/affiliates\/ref.php?u=boon86&l=http%3A%2F%2Fwww.getafreelancer.com%2Fprojects%2FCopywriting%2Fwebsubmissionarticles.html\",\"start_unixtime\":1252386528,\"start_date\":\"Tue, 08 Sep 2009 01:08:48 -0400\",\"end_unixtime\":1252559328,\"end_date\":\"Thu, 10 Sep 2009 01:08:48 -0400\",\"buyer\":{\"url\":\"http:\/\/www.getafreelancer.com\/affiliates\/ref.php?u=boon86&l=http%3A%2F%2Fwww.getafreelancer.com%2Fusers%2F39628.html\",\"id\":39628,\"username\":\"seexpert\"},\"state\":\"A\",\"short_descr\":\"need some1 to rewrite articles by taking them from the net from various min 5 sources. he\/she can draft...\",\"options\":{\"featured\":0,\"nonpublic\":0,\"trial\":0,\"fulltime\":0,\"for_gold_members\":0,\"hidden_bids\":0},\"budget\":{\"min\":30,\"max\":250},\"jobs\":[\"Copywriting\"],\"bid_stats\":{\"count\":0,\"avg\":false}}]}}"; 

PHP a un support JSON natif. Vous pouvez décoder la chaîne JSON directement dans un tableau associatif PHP en utilisant json_decode():

$result = json_decode($json_string, true); 

Test:

echo $result['projects']['items'][0]['name']; 
echo '<br>'; 
echo $result['projects']['items'][0]['url']; 

Résultat:

SEO 
http://www.getafreelancer.com/affiliates/ref.php?u=boon86&l=http%3A%2F%2Fwww.getafreelancer.com%2Fprojects%2FSEO%2FSEO.502817.html 


MISE À JOUR:

Répondre à la question: Que faire si j'ai beaucoup de projets dans le flux JSON et que je veux boucler chacun d'entre eux dans mysql.

Ce n'est vraiment pas lié à JSON, mais plutôt à des tableaux PHP. Il y a beaucoup de fonctions pratiques intégrées dans PHP que vous pouvez utiliser. Dans ce cas, tout ce dont vous avez besoin est de connaître le nombre d'éléments du tableau pour vos projets (lire: nombre de projets), puis extraire toutes les données de votre tableau en utilisant une simple boucle.

for($i = 0; $i < count($result['projects']['items']); $i++) { 
    $project_name = $result['projects']['items'][$i]['name']; 
    $project_id = $result['projects']['items'][$i]['id']; 
    $project_date = $result['projects']['items'][$i]['start_date']; 

    // save/print/do whatever you want with this data 
    echo "Project : $project_name [$project_id] - $project_date<br>"; 
} 

P.S. Nice PHP étape par étape tutoriel est here.

+0

merci, que faire si j'ai beaucoup de projets, dans le flux JSON et je veux boucler chaque 1 pour stocker dans mysql .. –

+0

Vérifiez la mise à jour. –