Parse curl response from github api - php

I'm a newbie in php but I'm trying to learn right now. What i want to make :
Send a curl request to github api like this :
curl_setopt($ch, CURLOPT_URL, 'https://api.github.com/legacy/repos/search/language:' . $lang);
And when i receive the result to display like a nice html page. The response that I'm receiving right now is displayed like the one written in github api documentation http://developer.github.com/v3/search/.
This is the first time I'm trying to learn PHP, but not the first time I've been web developing ( I've been contributing to a Hakyll based blog these weeks ).
My question is : How could i parse the results to format them nicely in a html page ?

The results are returned via JSON. You can make use of json_decode() for that.
Pass your cURL $response to this function. Such that print_r(json_decode($response,1));
Example of how to do it
<?php
$json='{
"text_matches": [
{
"object_url": "https://api.github.com/repositories/3081286",
"object_type": "Repository",
"property": "name",
"fragment": "Tetris",
"matches": [
{
"text": "Tetris",
"indices": [
0,
6
]
}
]
},
{
"object_url": "https://api.github.com/repositories/3081286",
"object_type": "Repository",
"property": "description",
"fragment": "A C implementation of Tetris using Pennsim through LC4",
"matches": [
{
"text": "Tetris",
"indices": [
22,
28
]
}
]
}
]
}';
$jarr=json_decode($json,1);
echo $jarr['text_matches'][0]['object_url']; //"prints" https://api.github.com/repositories/3081286

Related

I want a more consolidate json format

I am getting json array after getting applying query logic.
[
{
"id": "3",
"diag_name": "LT Diagnostics",
"test_name": "Alk PO4",
"booking_date": "2018-05-20"
},
{
"id": "3",
"diag_name": "LT Diagnostics",
"test_name": "CRP",
"booking_date": "2018-05-20"
},
{
"id": "4",
"diag_name": "Seepz Diagnostics",
"test_name": "Alk PO4",
"booking_date": "2018-05-21"
}
]
But i want a more justified json array written below.
[
{
"diag_name": "LT Diagnostics",
"test_name": [
{
"id": "3",
"name" : "Alk PO4"
},
{
"id": "3",
"name" : "CRP"
}
],
"booking_date": "2018-05-20"
},
{
"diag_name": "Seepz Diagnostics",
"test_name": [
{
"id": "4",
"name" : "Alk PO4"
}
],
"booking_date": "2018-05-21"
},
]
I am not getting it,How to do in php. I want a more consolidate json format.
Have you tried changing your SQL query to group by diag_name and booking_date? That would be the first step I’d employ to get the outer data.
Formatting the data in the nested manner you’re after could be a function of whatever record serializer you’re using — does it support nested JSON as a return type, or only flat JSON as your example return value shows?
If the record set -> JSON serializer only ever returns flat data, the comments above are correct that you will have to write your own formatter to change the shape of the JSON yourself...
The accepted answer of this other question may be of help:
Create multi-level JSON with PHP and MySQL
I'm not a PHP guy but this is a typical scenario to use functional programming by means of the monad Map.
Looking online I've found this article that could help you.
Changing datasource output is not always (seldom indeed) a viable option.
Enjoy coding

GitHub API - Highlighting Code

I'm using cURL in PHP to search through code on GitHub. I have everything working except one small option that i cannot figure out how to do.
I have passed the header: Accept: application/vnd.github.v3.text-match+json into cURL just fine, which gives me the extra [text_match] section like so:
{
"text_matches": [
{
"object_url": "https://api.github.com/repositories/167174/contents/src/attributes/classes.js?ref=825ac3773694e0cd23ee74895fd5aeb535b27da4",
"object_type": "FileContent",
"property": "content",
"fragment": ";\n\njQuery.fn.extend({\n\taddClass: function( value ) {\n\t\tvar classes, elem, cur, clazz, j, finalValue",
"matches": [
{
"text": "addClass",
"indices": [
23,
31
]
}
]
},
{
"object_url": "https://api.github.com/repositories/167174/contents/src/attributes/classes.js?ref=825ac3773694e0cd23ee74895fd5aeb535b27da4",
"object_type": "FileContent",
"property": "content",
"fragment": ".isFunction( value ) ) {\n\t\t\treturn this.each(function( j ) {\n\t\t\t\tjQuery( this ).addClass( value.call( this",
"matches": [
{
"text": "addClass",
"indices": [
80,
88
]
}
]
}
]
}
I use the $item['text_matches][$i]['fragment'] to refer to the line of code that i'm searching for.
What i was hoping to do was explicitly state what exact line in the file where the code fragment begins.
Originally, i thought $item['text_matches][$i]['matches]['indices'] was what i was looking for, but this is not the case.
So, how would i go about getting the exact line, because i dont see it within the json dataset.

Retrieving tasks within teams in PHP

I am trying to develop a web application that can fetch data from Asana and generate custom spreadsheet reports. This wrapper class was very helpful in making things simple.
However, I am having a hard time in writing code that gets me the team/s that a particular task belongs to. Even when I export data as JSON through Asana's web application the 'teams' find no mention. From what I understand, Asana itself does not provide an association between teams and tasks. Please correct me if I am wrong.
But if I am right at my conclusion, is there a workaround I could use? Teams are an important part of my data rendering and I need them to be mapped correctly in my reports that I am trying to generate from Asana. The report I want to generate would be hierarchical in nature.
Organisation
Team
Projects
Tasks
Subtask
Can I do something to achieve this hierarchy? The only place I get stuck is getting the projects under a particular team.
Glad to hear that you found that wrapper useful. We will be releasing a PHP Library ourselves soon that you may be interested in. Stay tuned!
Below is some pseudo-code to derive the hierarchy you are looking for, I think. Let me know if it helps.
GET /workspaces
{
"data": [
{
"id": 1234,
"name": "Startup Inc"
}
]
}
GET /workspaces/1234
{
"data": {
"id": 1234,
"name": "Startup Inc",
"is_organization": true,
...
}
}
Because is_corganization is true, we can then continue...
GET /organizations/organization-id/teams
{
"data": [
{
"id": 9876,
"name": "Ninja Team"
}
]
}
GET /teams/9876/projects
{
"data": [
{
"id": 5678,
"name": "Stealth Project"
}
]
}
GET /projects/5678/tasks
{
"data": [
{
"id": 8675309,
"name": "Top secret video"
}
]
}
GET /tasks/8675309
{
"data": {
"id": 8675309,
"created_at": "2015-03-25T17:28:59.255Z",
"modified_at": "2015-05-15T03:13:28.754Z",
"name": "Top secret video",
"notes": "https://www.youtube.com/watch?v=6WTdTwcmxyo",
"completed": false,
... # All the task data
]
}
}

json decode and get value using PHP

I have a JSON string and i want to get the value.
$s='{
"subscriptionId" : "51c04a21d714fb3b37d7d5a7",
"originator" : "localhost",
"contextResponses" : [
{
"contextElement" : {
"attributes" : [
{
"name" : "temperature",
"type" : "centigrade",
"value" : "26.5"
}
],
"type" : "Room",
"isPattern" : "false",
"id" : "Room1"
},
"statusCode" : {
"code" : "200",
"reasonPhrase" : "OK"
}
}
]
}';
Here is the code which I used but it didn't work.
$result = json_decode($s,TRUE); //decode json string
$b=$result ['contextResponses']['contextElement']['value']; //get the value????
echo $b;
ContextResponses contains a numerically indexed array (of only one item) and value property is more deeply nested than what you are trying to reference (it is within attributes array). This would appear to be what you need:
$b = $result['contextResponses'][0]['contextElement']['attributes'][0]['value'];
When reading a JSON-serialiazed data structure like that, you need to make sure and note every opening [ or { as they have significant meaning in regards to how you need to reference the items that follow it. You also may want to consider using something like var_dump($result) in your investigations, as this will show you the structure of the data after it has been deserialized, oftentimes making it easier to understand.
Also, proper indention when looking at something like this would help. Use something like http://jsonlint.com to copy/paste your JSON for easy reformatting. If you had your structure like the following, nesting levels become more readily apparent.
{
"subscriptionId": "51c04a21d714fb3b37d7d5a7",
"originator": "localhost",
"contextResponses": [
{
"contextElement": {
"attributes": [
{
"name": "temperature",
"type": "centigrade",
"value": "26.5"
}
],
"type": "Room",
"isPattern": "false",
"id": "Room1"
},
"statusCode": {
"code": "200",
"reasonPhrase": "OK"
}
}
]
}

How to turn JSON to object literal using PHP and jquery

Is there a concise neat way to turn an XML feed into an JavaScript object literal?
I have this XML feed
<jobs>
<industry>
<name>Technology</name>
<area>Refrigiration</area>
<area>Information Technology</area>
<area>Electronics</area>
<area>Avionics</area>
</industry>
<industry>
<name>Agriculture</name>
<area>Agri-Tourism</area>
<area>Animal Husbandry</area>
<area>Harvesting</area>
<area>Poultry</area>
</industry>
</jobs>
and wish to turn it to:
var jobs = [
{
"name" : "Technology",
"areas" : [ "Refrigiration" , "Information Technology", "Electronics", "Avionics" ]
},
{
"name" : "Agriculture",
"areas" : [ "Agri-Tourism" , "Animal Husbandry", "Harvesting", "Poultry" ]
},
{
"name" : "Media",
"areas" : [ "Journalism" , "Camera person", "Reality tv person", "Commentator" ]
}
];
I succeeded in encoding the JSON object using php. What I am missing is the rest.
echo json_encode(simplexml_load_string("<jobs>
<industry>
<name>Technology</name>
<area>Refrigiration</area>
<area>Information Technology</area>
<area>Electronics</area>
<area>Avionics</area>
</industry>
<industry>
<name>Agriculture</name>
<area>Agri-Tourism</area>
<area>Animal Husbandry</area>
<area>Harvesting</area>
<area>Poultry</area>
</industry>
</jobs>"));
This gives you:
{
"industry": [
{
"name": "Technology",
"area": [
"Refrigiration",
"Information Technology",
"Electronics",
"Avionics"
]
},
{
"name": "Agriculture",
"area": [
"Agri-Tourism",
"Animal Husbandry",
"Harvesting",
"Poultry"
]
}
]
}
You need to convert your XML to an array, https://stackoverflow.com/questions/4844476/xml-to-php-array
Then you'll need to convert the array to json using php json_encode()
If you are receiving a JSON encoded string in your javascript callback, you probably need to run $.parseJSON() to make jQuery treat it as a JSON object instead of a string
Two solutions:
PURE JQUERY
Using jQuery's parseXML , combined with jQuery get you can have the object you need:
$.get("http://www.example.com/path/to/file.xml", function(data){
var xml = $.parseXML(data);
console.log(xml);
});
PHP+JQUERY
if you've already parsed the object into a json, just print it into an html file and get that file using jquery's getJSON
$.getJSON("http://www.example.com/json/feed",function(data){
console.log(data);
});

Categories