JSON डेटा प्राप्त करने और डेटा को डीकोड करने के लिए cURL का उपयोग कैसे करें?


95

इसलिए मेरे पास एक लिंक है जो एक jSON ऑब्जेक्ट देता है, और मुझे इसे डीकोड करके PHP में वैरिएबल में डालना होगा।

यूआरएल

api.php?action=getThreads&hash=123fajwersa&node_id=4&order_by=post_date&order=desc&limit=1&grab_content&content_limit=1

यही वह वस्तु है जो इसे लौटाती है

{
    "count": 1,
    "threads": {
        "38752": {
            "thread_id": 38752,
            "node_id": 4,
            "title": "The ShadyCraft Beta Launch!",
            "reply_count": 45,
            "view_count": 946,
            "user_id": 2,
            "username": "Shady",
            "post_date": 1366956695,
            "sticky": 0,
            "discussion_state": "visible",
            "discussion_open": 1,
            "discussion_type": "",
            "first_post_id": 226167,
            "first_post_likes": 7,
            "last_post_date": 1369094302,
            "last_post_id": 228226,
            "last_post_user_id": 2,
            "last_post_username": "Shady",
            "prefix_id": 19,
            "tinhte_xentag_tags": "a:4:{i:0;s:9:\"minecraft\";i:2;s:4:\"news\";i:3;s:14:\"private server\";i:1;s:10:\"shadycraft\";}",
            "content": {
                "count": 1,
                "content": {
                    "226167": {
                        "post_id": 226167,
                        "thread_id": 38752,
                        "user_id": 2,
                        "username": "Shady",
                        "post_date": 1366956695,
                        "message": "[ATTACH=full]4143[\/ATTACH]\n \nWe've completely restructured ShadyCraft, and today will be the launch of the ShadyCraft beta.\n \nCurrent Features\n[LIST]\n[*]Towns\n[*]Nations\n[*]All out wars\n[*]A live update map\n[*]No White-list\n[*]Earn Money\n[*]Griefing allowed where possible\n[\/LIST]\nThese are just some features which have a lot more things behind them. For instance, there is town and nation upkeep, tax, kingdoms, mayors, and kings.\n \nWe really wanted to have the server self-governed and this is why griefing and PvP are allowed where ever they are possible. All towns and nations cannot be griefed by other members. You can create a town and buy plots for it and expand the town as you wish.\n \n[SIZE=4]All of this is shown in the live updating map located [URL='http:\/\/50.7.6.116:8123']here[\/URL].[\/SIZE]\n \n[SIZE=4][SIZE=6]Join the Beta Now[\/SIZE][\/SIZE]\n[SIZE=6]Server-IP: 50.7.6.116[\/SIZE]\n \n \n[USER=118053]Frenchy[\/USER] and [USER=4863]Wolfbane[\/USER]",
                        "ip_id": 747429,
                        "message_state": "visible",
                        "attach_count": 1,
                        "position": 0,
                        "likes": 7,
                        "like_users": "a:5:{i:0;a:2:{s:7:\"user_id\";i:105699;s:8:\"username\";s:6:\"kvothe\";}i:1;a:2:{s:7:\"user_id\";i:146724;s:8:\"username\";s:12:\"Graveyard219\";}i:2;a:2:{s:7:\"user_id\";i:70182;s:8:\"username\";s:9:\"wmbrown18\";}i:3;a:2:{s:7:\"user_id\";i:5473;s:8:\"username\";s:9:\"Oblivious\";}i:4;a:2:{s:7:\"user_id\";i:118053;s:8:\"username\";s:7:\"Frenchy\";}}",
                        "warning_id": 0,
                        "warning_message": "",
                        "anonymous_posting_real_user_id": 0,
                        "anonymous_posting_real_username": ""
                    }
                }
            }
        }
    }
}

मुझे वास्तव में केवल दिलचस्पी है

"title":"The ShadyCraft Beta Launch!",  
"reply_count:"45, 
"view_count": 946,  
"user_id":2, 
"username":"Shady", 
"post_date":1366956695, 
"sticky":0, 
"discussion_state":"visible",
"discussion_open":1,

और अंत में संदेश

[ATTACH=full]4143[/ATTACH]

We've completely restructured ShadyCraft, and today will be the launch of the ShadyCraft beta.

Current Features
[LIST]
[*]Towns
[*]Nations
[*]All out wars
[*]A live update map
[*]No White-list
[*]Earn Money
[*]Griefing allowed where possible
[/LIST]
These are just some features which have a lot more things behind them. For instance, there is town and nation upkeep, tax, kingdoms, mayors, and kings.

We really wanted to have the server self-governed and this is why griefing and PvP are allowed where ever they are possible. All towns and nations cannot be griefed by other members. You can create a town and buy plots for it and expand the town as you wish.

[SIZE=4]All of this is shown in the live updating map located [URL='http://50.7.6.116:8123']here[/URL].[/SIZE]

[SIZE=4][SIZE=6]Join the Beta Now[/SIZE][/SIZE]
[SIZE=6]Server-IP: 50.7.6.116[/SIZE]


[USER=118053]Frenchy[/USER] and [USER=4863]Wolfbane[/USER]

तो मैं jSON ऑब्जेक्ट कैसे निकाल सकता हूं और इसे PHP में सही चर में डाल सकता हूं, जिसे मैं बाद में उपयोग कर सकता हूं?

की तरह चर: $username, $user_id, $message, $title, $discussionState, और पर इतना।

मुझे बस यह जानने की जरूरत है कि मैं jSON ऑब्जेक्ट को कैसे पुनः प्राप्त कर सकता हूं, फिर PHP में चर में डेटा निकालें।

मैं अब PHP सरणी प्राप्त करने में सक्षम हूं, लेकिन मुझे सही मानों को कॉल करने में कुछ परेशानी हो रही है। यहाँ सरणी है।

s काफी समय से है जब से हमने फ़ैनटाइम फ़ोरम को मुख्य साइट के साथ एकीकृत करना शुरू किया था। हमने अब फ़ाइमाइम फ़ोरम और मुख्य साइट के साथ एकीकरण समाप्त कर दिया है। अब आप ध्यान नहीं देंगे कि फ़ैनटाइम चलाने वाले दो प्लेटफ़ॉर्म हैं, लेकिन केवल एक के बजाय। हमारा अगला कदम मंचों को थीम देना है ताकि वे मुख्य साइट की तरह दिख सकें! [ip_id] => 268 [message_state] => दृश्यमान [संलग्न_काउंट] => 0 [स्थिति] => 0 [पसंद] = = 0 [like_users] => a: 0: {} [चेतावनी_नि: शुल्क => 0 [चेतावनी_मेसेज] =>))))))

अब कहते हैं कि इस ऐरे को $arrayतब नामित किया गया था जब पहला तत्व मान प्राप्त करने के लिए "[गिनती]" मैं केवल निम्नलिखित नहीं कह सकता: print $array["[count]"] <- यह एक त्रुटि देता है।

उस तत्व के बारे में जो एक सरणी के रूप में एक मूल्य है, जो कि [threads]तत्व है। मैं कैसे प्राप्त करूं, शायद [thread_id]तत्व का मूल्य?

जवाबों:


266

मुझे लगता है कि यह आपके प्रश्न का उत्तर देगा: पी

$url="https://.../api.php?action=getThreads&hash=123fajwersa&node_id=4&order_by=post_date&order=‌​desc&limit=1&grab_content&content_limit=1";

का उपयोग करते हुए cURL

//  Initiate curl
$ch = curl_init();
// Will return the response, if false it print the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Set the url
curl_setopt($ch, CURLOPT_URL,$url);
// Execute
$result=curl_exec($ch);
// Closing
curl_close($ch);

// Will dump a beauty json :3
var_dump(json_decode($result, true));

का उपयोग करते हुए file_get_contents

$result = file_get_contents($url);
// Will dump a beauty json :3
var_dump(json_decode($result, true));

एक्सेस करना

$array["threads"][13/* thread id */]["title"/* thread key */]

तथा

$array["threads"][13/* thread id */]["content"/* thread key */]["content"][23/* post id */]["message" /* content key */];

1
मैंने छोटी सी कठिनाई के साथ सवाल को अद्यतन किया है जो मुझे PHP सरणी के साथ काम कर रहा है जो कि jSON द्वारा लौटाया गया है। यदि आप कर सकते हैं तो कृपया इसके निचले भाग को पढ़ें।
माज़

नमस्ते! मैं आपके उत्तर के लिए एक समान समस्या के साथ करीब हो रहा हूं, लेकिन अभी भी अटका हुआ है, क्या आप एक बार देख लेते हैं? stackoverflow.com/questions/28033267/…
लियोन गबन

ये सही है। CURLOPT_RETURNTRANSFERमुझे सही परिणाम दें (y) धन्यवाद!
अरिंदी

घंटों बिताने के बाद, लाइन को जोड़ना // SSL सत्यापन curl_setopt को अक्षम करें ($ ch, CURLOPT_SSL_VERIFYPEER, गलत); परिणाम देता है।
काटा

10

ऑब्जेक्ट प्राप्त करने के लिए आपको cURL का उपयोग करने की आवश्यकता नहीं है (आप मेमोरी में एक और dll लोड कर रहे हैं और एक और निर्भरता है, जब तक आपको वास्तव में कर्ल की आवश्यकता नहीं होगी मैं बिल्ट इन php फ़ंक्शंस के साथ), आप एक साधारण php file_get_contents (url) का उपयोग कर सकते हैं समारोह: http://il1.php.net/manual/en/function.file-get-contents.php

$unparsed_json = file_get_contents("api.php?action=getThreads&hash=123fajwersa&node_id=4&order_by=post_date&order=desc&limit=1&grab_content&content_limit=1");

$json_object = json_decode($unparsed_json);

यदि आप trueदूसरे पैरामीटर से गुजरते हैं तो jSON_decode () JSON को एक PHP ऑब्जेक्ट या एक सरणी में जोड़ता है। http://php.net/manual/en/function.json-decode.php

उदाहरण के लिए:

$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';

var_dump(json_decode($json));           // Object
var_dump(json_decode($json, true));     // Associative array

यूआरएल के बारे में क्या? मैं पहली जगह में jSON ऑब्जेक्ट को कैसे पुनः प्राप्त करूँ और इसे $ json वैरिएबल पर सेट करूँ? Url है: /api.php?action=getThreads&hash=123fajwersa&node_id=4&order_by=post_date&order=desc&limit=1&grab_content&content_limit=1 जो jSON ऑब्जेक्ट लौटाता है।
माज

1
मैंने छोटी सी कठिनाई के साथ सवाल को अद्यतन किया है जो मुझे PHP सरणी के साथ काम कर रहा है जो कि jSON द्वारा लौटाया गया है। यदि आप कर सकते हैं तो कृपया इसके निचले भाग को पढ़ें।
माज़

यह गलत है। आप file_get_contents () के लिए एक URL पास नहीं कर सकते ... इसे फ़ाइल नाम (पथ) होना चाहिए।
हेनरिक पीटरसन

@HenrikPetterson: file_get_contents का उपयोग केवल स्थानीय फाइलें ही नहीं, बल्कि URL लाने के लिए भी किया जा सकता है। हालांकि cURL अधिक विकल्प प्रदान करता है और संभवतः सभी के लिए एक बेहतर विकल्प है लेकिन सबसे सरल उपयोग के मामले हैं। अधिक के लिए यहां देखें: php.net/manual/en/function.file-get-contents.php
ineedhelp

7

आप इसे कर्ल के लिए उपयोग कर सकते हैं:

function fakeip()  
{  
    return long2ip( mt_rand(0, 65537) * mt_rand(0, 65535) );   
}  

function getdata($url,$args=false) 
{ 
    global $session; 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL,$url); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("REMOTE_ADDR: ".fakeip(),"X-Client-IP: ".fakeip(),"Client-IP: ".fakeip(),"HTTP_X_FORWARDED_FOR: ".fakeip(),"X-Forwarded-For: ".fakeip())); 
    if($args) 
    { 
        curl_setopt($ch, CURLOPT_POST, 1); 
        curl_setopt($ch, CURLOPT_POSTFIELDS,$args); 
    } 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    //curl_setopt($ch, CURLOPT_PROXY, "127.0.0.1:8888"); 
    $result = curl_exec ($ch); 
    curl_close ($ch); 
    return $result; 
} 

इसके बाद Json पढ़ें:

$result=getdata("https://example.com");

फिर :

///Deocde Json
$data = json_decode($result,true);
///Count
             $total=count($data);
             $Str='<h1>Total : '.$total.'';
             echo $Str;
//You Can Also Make In Table:
             foreach ($data as $key => $value)
              {
          echo '  <td><font  face="calibri"color="red">'.$value[type].'   </font></td><td><font  face="calibri"color="blue">'.$value[category].'   </font></td><td><font  face="calibri"color="green">'.$value[amount].'   </font></tr><tr>';

           }
           echo "</tr></table>";
           }

आप यह भी उपयोग कर सकते हैं:

echo '<p>Name : '.$data['result']['name'].'</p>
      <img src="'.$data['result']['pic'].'"><br>';

उम्मीद है कि इससे मदद मिली।


इससे मुझे मदद मिली। लेकिन मुझे अपने उपयोग के मामले में foreach आदानों में $ डेटा ['परिणाम'] के साथ $ डेटा को बदलना पड़ा।
इवाब


0

आप भी उपयोग कर सकते हैं

$result = curl_exec($ch);
return response()->json(json_decode($result));

हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.