In a previous blog post, we shared a tutorial on how to get started with Amazon Mechanical Turk (MTurk) and the PHP programming language. This blog post builds on that tutorial to show how to create Human Intelligence Tasks (HITs) in MTurk using the PHP programming language and MTurk’s Application Programming Interface (API).

If you didn’t already step through the previous tutorial, we suggest you start there. You can find it here: https://blog.mturk.com/tutorial-using-the-mturk-api-with-the-aws-sdk-for-php-472dc70dc4c3

Updating our code to call the MTurk Developer Sandbox

When we left off in the previous tutorial, our code called MTurk’s API to invoke the GetAccountBalance method. Here’s a quick recap of that code:

<?php
 require './aws/aws-autoloader.php';
$mTurk = new Aws\MTurk\MTurkClient([
     'version' => 'latest',
     'region'  => 'us-east-1'
 ]);
  
 $balance = $mTurk->getAccountBalance()->get('AvailableBalance');
 echo "Your available balance is: $" . $balance . PHP_EOL 
?>

The first thing we’ll do is make this code above call the MTurk Developer Sandbox. We’ll do that by adding the line of code below shown in bold:

...
$mTurk = new Aws\MTurk\MTurkClient([
     'version' => 'latest',
     'region'  => 'us-east-1',
     'endpoint' => 'https://mturk-requester-sandbox.us-east-1.amazonaws.com'
 ]);
...

If you re-run the entire code above (which we’ve stored in a file called mturk.php then you should see the following):

php mturk.php
Your available balance is: $10000.00

Excellent. Now your code is now set up to call the MTurk Developer Sandbox. All the HITs you create in this environment will not incur any costs, all reward amounts will be simulated, and Workers generally won’t access these tasks (since no rewards are paid). Next, we’ll add code to create our first HIT.

Creating HITs with PHP

Next, we’ll add the following code to our main file. This code will load a file containing our question, then it will call the MTurk API’s CreateHIT method. Simply add the lines shown in bold to the bottom of your file:

<?php
 require './aws/aws-autoloader.php';
$mTurk = new Aws\MTurk\MTurkClient([
   'version' => 'latest',
   'region'  => 'us-east-1',
   'endpoint' => 'https://mturk-requester-sandbox.us-east-1.amazonaws.com'
 ]);
  
 $balance = $mTurk->getAccountBalance()->get('AvailableBalance');
 echo "Your available balance is: $" . $balance . PHP_EOL;
  
 // Open and read the question file
 $question = file_get_contents("question.xml") or dir("Read error");
 
 // Create the HIT 
 $hit = $mTurk->createHIT([
    'Title' => 'Test Title',
    'Description' => 'Test Description',
    'Reward' => '0.50', // $0.50 reward for completing this task
    'AssignmentDurationInSeconds' => (60 * 60), // 60s*60min = 1 hr
    'LifetimeInSeconds' => 60 * 60 * 24, // 60s*60m*24h = 1 day
    'Question' => $question,
 ]);
 
 echo "Your HIT was created. You can view it here: " . PHP_EOL;
 echo "https://workersandbox.mturk.com/projects/" . $hit["HIT"]["HITTypeId"] . "/tasks" . PHP_EOL;
?>

Before we run our code again, we’ll need to define a question. The code above opens a file called question.xml. We’re going to create that file next.

Creating our Question file

Let’s create a file called question.xml. We’ll create that file and place it in the same directory as your mturk.php file. In that file, place the following code:

<HTMLQuestion xmlns="http://mechanicalturk.amazonaws.com/AWSMechanicalTurkDataSchemas/2011-11-11/HTMLQuestion.xsd">
  <HTMLContent><![CDATA[
  <!DOCTYPE html>
    <html>
    <head>
      <meta http-equiv='Content-Type' content='text/html; charset=UTF-8'/>
      <script type='text/javascript' src='https://s3.amazonaws.com/mturk-public/externalHIT_v1.js'> .   
      </script>
    </head>
    <body>
      <form name='mturk_form' method='post' id='mturk_form' action='https://www.mturk.com/mturk/externalSubmit'>
        <input type='hidden' value='' name='assignmentId' id='assignmentId'/>
        <h1>This is a test question</h1>
        <p><textarea name='comment' cols='80' rows='3'></textarea></p>
        <p><input type='submit' id='submitButton' value='Submit' /></p></form>
        <script language='Javascript'>turkSetAssignmentID();    
        </script>
      </body>
    </html>
    ]]>
  </HTMLContent>
  <FrameHeight>450</FrameHeight>
</HTMLQuestion>

Once you save this file, next we’ll run the code. It will still display our account balance, but will also create a HIT in the MTurk Developer Sandbox and give us a link to access it. We’ll run it by typing the command below at the command line:

php mturk.php

You should see the following output:

Your available balance is: $10000.00
You can see your HIT at:
https://workersandbox.mturk.com/projects/3ABSYNXI561JDBHCCQ3ZW0IA0RBWM6/tasks

By copying and pasting the link shown, you should be taken to the MTurk Worker Sandbox site. You should be able to see your HITs there. It should look similar to the following:

Congratulations! You’ve successfully created a HIT using the MTurk API and the AWS SDK for PHP.

Wrapping up

Today, we’ve just scratched the surface of what’s possible with the AWS SDK for PHP and the MTurk Requester API. In future tutorials, we’ll continue to build on this to show readers how to do even more.

This tutorial was originally published on Happenings at MTurk.

This entry was posted in Help and tagged .

Leave a Reply

Your email address will not be published. Required fields are marked *