PHP configuration

Configuration

In shared_workforce.inc.php

$shared_workforce_url = 'https://api.sharedworkforce.com/tasks';
$shared_workforce_api_key = "05a0a234-499a-4010-beb8-bfdd4e0234b0";

function create_shared_workforce_task($data) {
	global $shared_workforce_url, $shared_workforce_api_key;

	$data = json_encode($data);

	$headers = array(
		'Accept: application/json',
		'Content-Type: application/vnd.com.sharedworkforce.text+json',
		"X-API-Key: $shared_workforce_api_key"
	);

	$handle = curl_init();
	curl_setopt($handle, CURLOPT_URL, $shared_workforce_url);
	curl_setopt($handle, CURLOPT_HTTPHEADER, $headers);
	curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);

	curl_setopt($handle, CURLOPT_POST, true);
	curl_setopt($handle, CURLOPT_POSTFIELDS, $data);

	$response = curl_exec($handle);
	$code = curl_getinfo($handle, CURLINFO_HTTP_CODE);
}  

Tagging text

In this example, we might have some text we would like to moderate.

tag_text.php:

require_once 'shared_workforce.inc.php';

$body_text = "Load your text to be moderated here";

create_shared_workforce_task(
	array('task' =>
		array(
			"title" 			   => "Classify text",
			"instruction"    => "Choose the first option that applies.",
			"text"           => $body_text, // profile text to be moderated
			"answer_type"    => "choice", // Could be set to "tags" for multiple selection
			"answer_options" => array(
				"meaningless words or repeated characters",
				"phone number or email address",
				"sexual",
				"bad language",
				"none of the above" // approve
			),
			"callback_url" => "http://www.example.com/tag_text_callback.php",
			"callback_params" => array('profile_id'=>1234)
		)
	)
);
  

tag_text_callback.php:

require_once 'shared_workforce.inc.php';

if($results = file_get_contents("php://input")) {

	$results = json_decode($results, true);
	if ($results['api_key'] != $shared_workforce_api_key) { die("Wrong API key!"); } 

	// check that the api key matches before doing anything
	$task = $results['task'];
	
	/*
	  Get the profile id that was moderated (set by you when the task is created)
	*/
	$photo_id = $task['callback_params']['profile_id'];

	/* 
	  responses is always an array, could be more than one response
	  if you have asked for multiple opinions. In this case we just
	  have the one response.
	*/
	$response = $task['responses'][0];

	/* Answer could be a String or Array. If it is a single
	   choice, it will be a string.
	*/
	$answer = $response['answer'];

	/* Your actions based on the answer */
	if ($answer == "phone number or email address") {
		// maybe do a shared workforce edit task?
	} else if ($anwer == "bad language") {
		// escalate etc.
	}
}
  

Editing text

In this example, we might have some text we would like to remove the contact details from.

edit_text.php:

require_once 'shared_workforce.inc.php';

$text_with_contact_details = "Find me on emm ess enn lisa one two three";

create_shared_workforce_task(
	array('task' =>
		array(
			"title" 	     => "Remove contact details from text",
			"instruction"    => "Please remove any contact details from the text.",
			"text"           => $text_with_contact_details,
			"answer_type"    => "edit",
			"callback_url" => "http://www.example.com/edit_text_callback.php",
			"callback_params" => array('profile_id'=>1234)
		)
	)
);
  

edit_text_callback.php:

require_once 'shared_workforce.inc.php';

if($results = file_get_contents("php://input")) {

	$results = json_decode($results, true);

	// check that the api key matches before doing anything
	if ($results['api_key'] != $shared_workforce_api_key) { die("Wrong API key!"); }
	$task = $results['task'];
	
	/*
	  Get the profile id that was moderated (set by you when the task is created)
	*/
	$profile_id = $task['callback_params']['profile_id'];

	/* 
	  responses is always an array, could be more than one response
	  if you have asked for multiple opinions. In this case we just
	  have the one response.
	*/
	$response = $task['responses'][0];

	/* Answer could be a String or Array. If it is a single
	   choice, it will be a string.
	*/
	$answer = $response['answer'];

	/* Update your profile with the new text.
	   Might be something like:
		 $profile = Profile.find($profile_id);
		 $profile->update(array('text'=>$answer));
	*/

}
  

Tagging images

In this example, we might have an image we'd like to check.

tag_image.php:

require_once 'shared_workforce.inc.php';

$image_url = "http://upload.wikimedia.org/wikipedia/commons/thumb/1/1b/Persian_Cat_%28kitten%29.jpg/513px-Persian_Cat_%28kitten%29.jpg";

create_shared_workforce_task(
	array('task' =>
		array(
			"title" 			   => "Please classify this photo",
			"instruction"    => "Choose the first answer that applies",
			"answer_type"    => "choice", // Single choice from a list of options
			"answer_options" => array(
				"poorly cropped with a border or background"
				"might be offensive",
				"wrong way round",
				"none of the above"
			),
			"image_url" => $image_url, // the url of the image to be moderated
			"callback_url" => "http://www.example.com/tag_image_callback.php", // the URL of the script to be called when complete
			"callback_params" => array('photo_id'=>1234) // set your photo id here so you can identify it later
		)
	)
);

tag_image_callback.php:

require_once 'shared_workforce.inc.php';

if($results = file_get_contents("php://input")) {

	$results = json_decode($results, true);
	if ($results['api_key'] != $shared_workforce_api_key) { die("Wrong API key!"); } # check that the api key matches before doing anything
	$task = $results['task'];
	
	/*
	  Get the photo id that was moderated (set by you when the task is created)
	*/
	$photo_id = $task['callback_params']['photo_id'];

	/* 
	  responses is always an array, could be more than one response
	  if you have asked for multiple opinions. In this case we just
	  have the one response.
	*/
	$response = $task['responses'][0];

	/* Answer could be a String or Array. If it is a single
	   choice, it will be a string.
	*/
	$answer = $response['answer'];

	/* Your actions based on the answer */
	if ($answer == "poorly cropped with a border or background") {
		// Approve. Create a cropping task
		create_shared_workforce_task(
			array('task' =>
				array(
			    "title" 			     => "Crop this photo",
			    "instruction"      => "Crop this photo, removing any borders or backgrounds.",
			    "answer_type"      => "crop",
			    "image_crop_ratio" => 1, // square (optional)
			    "image_url" => "http://upload.wikimedia.org/wikipedia/commons/thumb/1/1b/Persian_Cat_%28kitten%29.jpg/513px-Persian_Cat_%28kitten%29.jpg",
			    "callback_url" => "http://www.example.com/crop_photo_callback.php",
			    "callback_params" => array('photo_id'=>$photo_id)
				)
			)
		);
	} else if ($answer == "wrong way round") {
		// create a rotation task
		create_shared_workforce_task(
			array('task' =>
				array(
			    "title" 			   => "Rotate this photo",
			    "instruction"    => "Rotate the photo until it is the right way round",
			    "answer_type"    => "rotate",
			    "image_url" => "http://upload.wikimedia.org/wikipedia/commons/thumb/1/1b/Persian_Cat_%28kitten%29.jpg/513px-Persian_Cat_%28kitten%29.jpg", # the photo to be moderated
			    "callback_url" => "http://www.example.com/rotate_photo_callback.php",
			    "callback_params" => array('photo_id'=>$photo_id)
				)
			)
		);
	} else if ($answer == "might be offensive") {
		// Here you could delete the photo and send a warning email
	}
}
  

Cropping images

In this example, we might have a photo we would like to crop to a nice square.

crop_image.php:

require_once 'shared_workforce.inc.php';

$image = "http://upload.wikimedia.org/wikipedia/commons/thumb/1/1b/Persian_Cat_%28kitten%29.jpg/513px-Persian_Cat_%28kitten%29.jpg";

create_shared_workforce_task(
	array('task' =>
		array(
			"title" 	         => "Crop this photo",
			"instruction"      => "Please crop the photo around the face, keeping their chin visible and their eyes 1/3 of the way from the top",
			"image_url"        => $image_url,
			"answer_type"      => "crop",
			"image_crop_ratio" => 1, // Square
			"callback_url"     => "http://www.example.com/crop_image_callback.php",
			"callback_params"  => array('photo_id'=>1234)
		)
	)
);
  

crop_photo_callback.php:


require_once 'shared_workforce.inc.php';

if($results = file_get_contents("php://input")) {

	$results = json_decode($results, true);

	// check that the api key matches before doing anything
	if ($results['api_key'] != $shared_workforce_api_key) { die("Wrong API key!"); } 
	$task = $results['task'];
	
	// Get the photo id that was moderated (set by you when the task is created)
	$photo_id = $task['callback_params']['photo_id'];

	/* 
	  responses is always an array, could be more than one response
	  if you have asked for multiple opinions. In this case we just
	  have the one response.
	*/
	$response = $task['responses'][0];

	/* If the task answer type is edit or crop, there will be 
	   a URL with the cropped/rotated image for you to download
	*/
	$new_image_url = $response['new_image_url'];

	// Put the newly cropped image in to a file
	file_put_contents("cropped_image_$photo_id.jpg", file_get_contents($new_image_url));

}
  

Rotating images

In this example, we might have a photo that we would like to rotate to its correct orientation.

rotate_image.php:

require_once 'shared_workforce.inc.php';

$image = "http://upload.wikimedia.org/wikipedia/commons/thumb/1/1b/Persian_Cat_%28kitten%29.jpg/513px-Persian_Cat_%28kitten%29.jpg";

create_shared_workforce_task(
	array('task' =>
		array(
			"title" 	        => "Rotate this photo",
			"instruction"     => "Please rotate the photo until it is the right way up",
			"image_url"       => $image_url,
			"answer_type"     => "rotate",
			"callback_url"    => "http://www.example.com/rotate_image_callback.php",
			"callback_params" => array('photo_id'=>1234)
		)
	)
);
  

rotate_image_callback.php:


require_once 'shared_workforce.inc.php';

if($results = file_get_contents("php://input")) {

	$results = json_decode($results, true);

	// check that the api key matches before doing anything
	if ($results['api_key'] != $shared_workforce_api_key) { die("Wrong API key!"); } 
	$task = $results['task'];
	
	//  Get the photo id that was moderated (set by you when the task is created)
	$photo_id = $task['callback_params']['photo_id'];

	/* 
	  responses is always an array, could be more than one response
	  if you have asked for multiple opinions. In this case we just
	  have the one response.
	*/
	$response = $task['responses'][0];

	/* If the task answer type is edit or crop, there will be 
	   a URL with the cropped/rotated image for you to download
	*/
	$new_image_url = $response['new_image_url'];

	//  Put the newly rotated image in to a file
	file_put_contents("rotated_image_$photo_id.jpg", file_get_contents($new_image_url));

}