Code Monkey home page Code Monkey logo

phpsoda's Introduction

PhpSoda

Stable Release Build Status Scrutinizer Code Quality Coverage Status

A PHP library for working with the Socrata Open Data API. Provided as an alternative to Socrata's official implementation, this library fills the short-comings of the official library by providing more functionality, a more object-oriented approach, documentation, and plenty of example code.

This library fully supports interacting with the Socrata Open Data API (SODA) by getting datasets, handling tokens, handling basic authentication, and OAuth2.0 tokens in order to write or modify datasets.

Requirements

  • PHP 5.6+

Installation

This library is available on Packagist as allejo/php-soda, add it using Composer.

You're not using Composer? Don't worry, this library is also provided as a Phar archive for you include in your code. Get the latest Phar archive from our Releases.

Check out our wiki article if you require assistance with using this library.

Sample Usage

Here are some quick examples on how PhpSoda works, but there's a lot more you can do. Check out our wiki to see everything else.

Get a dataset

// Create a client with information about the API to handle tokens and authentication
$sc = new SodaClient("opendata.socrata.com");

// Access a dataset based on the API end point
$ds = new SodaDataset($sc, "pkfj-5jsd");

// Create a SoqlQuery that will be used to filter out the results of a dataset
$soql = new SoqlQuery();

// Write a SoqlQuery naturally
$soql->select("date_posted", "state", "sample_type")
     ->where("state = 'AR'")
     ->limit(1);

// Fetch the dataset into an associative array
$results = $ds->getDataset($soql);

Updating a dataset

// Create a client with information about the API to handle tokens and authentication
$sc = new SodaClient("opendata.socrata.com", "<token here>", "[email protected]", "password");

// The dataset to upload
$data = file_get_contents("dataset.json");

// Access a dataset based on the API end point
$ds = new SodaDataset($sc, "1234-abcd");

// To upsert a dataset
$ds->upsert($data);

// To replace a dataset
$ds->replace($data);

Note: This library supports writing directly to datasets with the Socrata Open Data API. For datasets with one or more data transformations applied to the schema through the Socrata Data Management Experience (the user interface for creating datasets), use the Socrata Data Management API to apply those same transformations to all updates. For more details on when to use SODA vs the Socrata Data Management API, see the Data Management API documentation

Getting Help

To get help, see if our wiki has any information regarding your question. If the wiki can't help you, you may either create an issue or stop by IRC; I'm available on IRC as "allejo" so feel free to ping me. I recommend creating an issue in case others have the same question but for quick help, IRC works just fine.

To report a bug or request a feature, please submit an issue.

IRC

Channel: #socrata-soda
Network: irc.freenode.net

Thank You

License

MIT

phpsoda's People

Contributors

allejo avatar ryan-hall avatar shravass avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

phpsoda's Issues

API Limits

  • API limits have been removed as of 2.1 datasets
  • Throw a warning/exception when a number greater than 50,000 is used on older datasets

HttpException class not returning any info

if (is_null($json))

Should the check have a ! in it?

The following gets passed into $response which does not json_decode and falls into the if block.


HTTP/1.1 100 Continue

HTTP/1.1 400 Bad Request
Server: nginx
Date: Mon, 24 Jun 2019 20:00:04 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
X-Socrata-RequestId: 316xuzmsevgwlsbdhjwwokmpw
Access-Control-Allow-Origin: *
X-Error-Code: invalid_request
X-Error-Message: Illegal field name sent: contractor_nid
Cache-Control: private, no-cache, must-revalidate
Age: 0
X-Socrata-Region: aws-us-east-1-fedramp-prod

{
  "code" : "invalid_request",
  "error" : true,
  "message" : "Illegal field name sent: contractor_nid"
}

clarify Socrata APIs referenced in readme and wiki documentation

Write operations to Socrata datasets with the Socrata Open Data API differ from write operations with the more recent Data Management API. Specifying which API this library uses helps prevent user confusion.

Adding something like:

Note: This library supports writing directly to datasets with the Socrata Open Data API. For datasets with one or more data transformations applied to the schema through the Socrata Data Management Experience (the user interface for creating datasets), use the Socrata Data Management API to apply those same transformations to all updates. For more details on when to use SODA vs the Socrata Data Management API, see the Data Management API documentation

to the readme and wiki would help clarify expectations when publishing to Socrata.

I'll open a PR with changes.

SQL select with Alaises

In original documentation , Select query with aliases is given the following explanation:
https://github.com/allejo/PhpSoda/wiki/Working-with-SoQL-Queries

I came across two issues while working with aliases:

  1. When a select is given with alias, the $select statement generated has white spaces instead of %20. As a result, the HTTP request fails with response of 400 error code

eg: $selectWithAliases = array(
"origin" => "origin_type"
);

URL formed: https://.../.../...?$select=origin AS origin
Expected URL: https://.../.../...?$select=origin%20AS%20origin

  1. While trying the case "Not all column names must have an alias", the column given without alias are generated like origin AS , file_type AS, year as publication_year instead of origin, file_type, year as publication_year

eg: $selectWithAliases = array(
"origin",
"file_type",
"year" => "publication_year"
);

URL formed: https://.../.../...?$select=origin AS ,file_type AS ,year AS publication_year
Expected URL: https://.../.../...?$select=origin,filer_name,year AS publication_year

A patch is required in line 169 of src/SoqlQuery.php to fix these 2 issues:

$formattedValues[] = (is_string($key) && (!is_null($value))) ? rawurlencode(sprintf($format, trim($key), trim($value))) : (is_string($key) ? $key : $value);

(is_string($key) && (!is_null($value))) ? rawurlencode(sprintf($format, trim($key), trim($value))) : (is_string($key) ? $key : $value) will handle the following cases:

  1. Select with key and value ( column with alias )
  2. Select with key and null value( column without alias while other column may come with alias)
  3. Select with value ( column without alias in any column of select statement)

Use PhpSoda without Composer

Hi,
I want to use the PHP PhpSoda library in my own project how can i add SodaClient in my index.php file

When i add "use allejo\Socrata\SodaClient;" and i instantiate $sc = new SodaClient("http://opendata.socrata.com"); it's give me the error ( ! ) Fatal error: Class 'allejo\Socrata\SodaClient' not found in C:\wamp\www\Socrata\PhpSoda\tests\index.php on line 6
Call Stack

Time Memory Function Location

1 0.0010 131408 {main}( ) ..\index.php:0
how can i resolve it please?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.