Skip to content

Instantly share code, notes, and snippets.

@davidphasson
Forked from bensie/batch_post.php
Created April 20, 2009 19:47
Show Gist options
  • Save davidphasson/98708 to your computer and use it in GitHub Desktop.
Save davidphasson/98708 to your computer and use it in GitHub Desktop.
#!/usr/bin/php
<?
$api_key = "xxxx";
$url = "xxxx";
foreach( glob("*.xml") as $filename)
{
$cmd = "curl -u $api_key:X -H 'Content-type: application/xml' -d \@$filename $url";
$result = `$cmd`;
print "$result\n";
sleep(1);
}
?>
#!/usr/bin/perl
# Sample Perl script for batch XML job posts
$api_key = "xxxxxx";
$url = "http://www.disabledperson.com/jobs.xml";
foreach( glob("*.xml") )
{
$cmd = "curl -u $api_key:X -H 'Content-Type: application/xml' -d \@$_ $url";
$result = `$cmd`
print "$result\n";
sleep 1;
}
# Sample Ruby script for batch XML job posts
api_key = "fe1db44cab667104866b74f0fd5da9eb80297590"
url = "http://www.disabledperson.com/jobs.xml"
Dir.glob("*.xml") do |file|
puts `curl -u #{api_key}:X -H 'Content-Type: application/xml' -d @#{file} #{url}`
sleep 1
end
Disabled Persons API
The Disabled Persons API is implemented as vanilla XML over
HTTP using all four verbs (GET/POST/PUT/DELETE).
You can explore the view part of the API (everything that's
fetched with GET) through a regular browser. Using Firefox
for this is particularly nice as it has a good, simple XML
renderer (unlike Safari which just strips the tags and dumps
the content). Pretty much any URL in Disabled Persons can
be viewed in its XML form by adding the .xml extension. So
/jobs/4 becomes /jobs/4.xml if you want to see the XML version.
Authentication:
When you're using the API, it's always through an existing
user registered through the Disabled Persons website. There's
no special API user. So when you use the API as "james", you
get to see and work with what "james" is allowed to.
Authenticating is done with an authentication token, which
you'll find on the "My Account" screen on the website when
you're logged in (click the "Reveal authentication token for
feeds/API" link).
When using the authentication token, you don't need a separate
password. But since Disabled Persons uses HTTP Basic
Authentication, and lots of implementations assume that you want
to have a password, it's often easier just to pass in a dummy
password, like X.
Example using the authentication token and a dummy password through curl:
curl -u 605b32dd:X http://disabledpersons.com/jobs/1.xml
Remember that anyone who has your authentication token can see and
change everything you have access to. So you want to guard that as
well as you guard your username and password. If you come to fear
that it has been compromised, just change your regular password and
the authentication token will change too.
Reading through the API:
The Disabled Persons API has two categories of actions for reading:
Show and list. Show returns a single record and list returns a collection.
There's usually just a single show action for each resource, but many
lists. All these actions are done through GET, which also means that
they're all easily explorable through a browser as described above.
A few examples of reading with curl:
curl -u 605b32dd:X http://disabledpersons.com/jobs/1.xml
If the read is successful, you'll get an XML response back along with the
status code "200 OK".
Writing through the API:
Creating, updating, and deleting resources through the API is almost as
easy as reading, but you can't explore it as easily through the browser.
Regardless of your implementation language, though, using curl to play
first is a great idea. It makes it very easy to explore the API and is
perfect for small scripts too.
When you're creating and updating resources, you'll be sending XML into
Disabled Persons. You need to let the system know that fact by adding the
header "Content-type: application/xml", so we know that it's not regular
form-encoded data coming in. Then you just include the XML of the resource
in the body of your request.
A few examples creating new resources, first with the XML inline, second
referencing the XML from a file:
curl -u 605b32dd:X -H 'Content-Type: application/xml' \
-d '<job><title>Assistant to the Regional Manager</title></job>' http://disabledperson.com/jobs.xml
curl -u 605b32dd:X -H 'Content-Type: application/xml' \
-d @job.xml http://disabledperson.com/jobs.xml
The response to a successful creation is the status code "201 Created".
You can get the URL of the new resource in the Location header (such
that you know where to update your new resource in the future). We also
include the complete XML for the final resource in the response. This is
because you can usually get away with creating a new resource with less
than all its regular attributes. Especially attributes like created_at
can be helpful to get back from the creation.
Updating resources is done through the PUT verb and against the URL of
the resource you want to update. A few examples:
curl -u 605b32dd:X -X PUT -H 'Content-Type: application/xml' \
-d <job><title>Assistant to the Regional Manager</title></job>' http://disabledperson.com/jobs/5.xml
curl -u 605b32dd:X -X PUT -H 'Content-Type: application/xml' \
-d @job.xml http://disabledperson.com/jobs/27.xml
The response to a successful update is "200 OK".
Finally, you can delete resources (if you're allowed to) using the DELETE
verb. An of that:
curl -u 605b32dd:X -X DELETE http://disabledperson.com/jobs/5.xml
Note that you don't need to pass the content-type header because you're
not sending any XML. The response to a successful delete is "200 OK".
The following job categories are permitted -- you cannot create your own.
If you send an invalid or misspelled category, the job will be assigned to 'Other'.
'Accounting', 'Admin - Clerical', 'Automotive', 'Banking', 'Biotech', 'Business Development',
'Business Opportunity', 'Construction', 'Consultant', 'Customer Service', 'Design', 'Distribution - Shipping',
'Education', 'Engineering', 'Entry Level', 'Executive', 'Facilities', 'Finance', 'Franchise', 'General Business',
'General Labor', 'Government', 'Government - Federal', 'Grocery', 'Health Care', 'Hospitality - Hotel',
'Human Resources', 'Information Technology', 'Installation - Maint - Repair', 'Insurance', 'Inventory', 'Legal',
'Legal Admin', 'Management', 'Manufacturing', 'Marketing', 'Media - Journalism - Newspaper', 'Nonprofit - Social Services',
'Nurse', 'Other', 'Pharmaceutical', 'Professional Services', 'Purchasing - Procurement', 'QA - Quality Control',
'Real Estate', 'Research', 'Restaurant - Food Service', 'Retail', 'Sales', 'Science', 'Skilled Labor - Trades',
'Strategy - Planning', 'Supply Chain', 'Telecommunications', 'Training', 'Transportation', 'Veterinary Services',
'Warehouse'
<?xml version="1.0" encoding="UTF-8"?>
<job>
<additional-info>We will be accepting applications for this job until May 1, 2009.</additional-info>
<apply-hits type="integer">0</apply-hits>
<career-level>3+ Years Experience</career-level>
<category-id type="integer">3</category-id>
<company-id type="integer">1</company-id>
<contact-email-address>noreply@example.com</contact-email-address>
<contact-fax-number>8585551212</contact-fax-number>
<contact-name>James Miller</contact-name>
<contact-phone-number>8585551212</contact-phone-number>
<contact-web-address>http://www.example.com</contact-web-address>
<created-at type="datetime">2009-04-20T18:17:31Z</created-at>
<description>Sales Associates play an important role in the success of their respective companies. Regardless of the type of product they sell, their primary duties are to interest buyers and purchasing agents in their merchandise or services and to address clients&#8217; questions and concerns. Sales associates demonstrate their products and advise clients on how using these products can reduce costs and/or increase revenue. Sales associates can spend much of their time traveling to and visiting with prospective buyers and current clients.</description>
<expiration-date type="datetime">2009-05-20T23:59:59Z</expiration-date>
<id type="integer">47</id>
<location-city>San Diego</location-city>
<location-state>CA</location-state>
<location-zip>92130</location-zip>
<maximum-salary type="decimal">15.0</maximum-salary>
<minimum-salary type="decimal">9.0</minimum-salary>
<number>285940TZ</number>
<payment-type>Hourly</payment-type>
<position-type>Full Time</position-type>
<publish-date type="datetime">2009-04-20T07:00:00Z</publish-date>
<title>Assistant To The Regional Manager</title>
<updated-at type="datetime">2009-04-20T18:17:31Z</updated-at>
<view-hits type="integer">0</view-hits>
</job>
<?xml version="1.0" encoding="UTF-8"?>
<job>
<!-- Required Fields -->
<number>285940TZ</number>
<title>Assistant To The Regional Manager</title>
<category-name>Sales</category-name>
<description>Sales Associates play an important role in the success of their respective companies. Regardless of the type of product they sell, their primary duties are to interest buyers and purchasing agents in their merchandise or services and to address clients’ questions and concerns. Sales associates demonstrate their products and advise clients on how using these products can reduce costs and/or increase revenue. Sales associates can spend much of their time traveling to and visiting with prospective buyers and current clients.</description>
<location-city>San Diego</location-city>
<location-state>CA</location-state>
<career-level>3+ Years Experience</career-level>
<position-type>Full Time</position-type>
<minimum-salary>9</minimum-salary>
<maximum-salary>15</maximum-salary>
<payment-type>Hourly</payment-type>
<!-- Optional Fields -->
<location-zip>92130</location-zip>
<additional-info>We will be accepting applications for this job until May 1, 2009.</additional-info>
<contact-name>James Miller</contact-name>
<contact-phone-number>8585551212</contact-phone-number>
<contact-fax-number>8585551212</contact-fax-number>
<contact-email-address>noreply@example.com</contact-email-address>
<contact-web-address>http://www.example.com</contact-web-address>
</job>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment