jackbot / PHP-Mechanical-Turk

A PHP wrapper for the Amazon Mechanical Turk REST API
MIT License
25 stars 10 forks source link

Example not working #4

Open nightrome opened 8 years ago

nightrome commented 8 years ago

I tried the example with my access keys, but there seem to be several errors. Some things I had to fix: [1] You didn't urlencode the "question", which results in a bug if it includes whitespaces. [2](and following) You forgot the brackets () around the ternary if-then-else operator. Therefore these fields are missing in the request..

Now at least the basic example works..

[1] https://github.com/jackbot/PHP-Mechanical-Turk/blob/master/MechanicalTurk.class.php#L59 [2] https://github.com/jackbot/PHP-Mechanical-Turk/blob/master/MechanicalTurk.class.php#L55

Now the createHit function looks like this:

` public function createHit($question, $params = null) {

    $ts = $this->Unix2UTC(time());
    $url = $this->startUrl();
    $url .= '&Operation=CreateHIT';
    $url .= '&Signature=' . $this->generateSignature($this->MTURK_SERVICE, 'CreateHIT', $ts);
    $url .= '&Timestamp=' . $ts;
    $url .= '&Title=' . (isset($params['title']) ? urlencode($params['title']) : urlencode($this->defaults['title']));
    $url .= '&Description=' . (isset($params['description']) ? urlencode($params['description']) : urlencode($this->defaults['description']));
    $url .= '&Reward.1.Amount=' . (isset($params['reward']) ? urlencode($params['reward']) : $this->defaults['reward']);
    $url .= '&Reward.1.CurrencyCode=' . (isset($params['reward_currency']) ? urlencode($params['reward_currency']) : $this->defaults['reward_currency']);
    $url .= '&Question=' . (($question != null) ? urlencode($question) : urlencode($defaults['question']));
    $url .= '&AssignmentDurationInSeconds=' . $this->defaults['duration'];
    $url .= '&AutoApprovalDelayInSeconds=' . $this->defaults['auto_approve'];
    $url .= isset($params['qualification_requirement']) ? $this->generateQualificationRequirement($params['qualification_requirement']) : $this->generateQualificationRequirement($this->defaults['qualification_requirement']);
    $url .= '&LifetimeInSeconds=' . (isset($params['lifetime']) ? urlencode($params['lifetime']) : $this->defaults['lifetime']);
    $url .= '&Keywords=' . (isset($params['keywords']) ? urlencode(implode(', ', $params['keywords'])) : urlencode(implode(', ', $this->defaults['keywords'])));
    if (isset($params['requester_annotation'])) {
        $url .= '&RequesterAnnotation=' . $params['requester_annotation'];
    } else if (isset($defaults['requester_annotation'])) {
        $url .= '&RequesterAnnotation=' . $defaults['requester_annotation'];
    }

    $response = file_get_contents($url);
    return $response;
}

`

jackbot commented 8 years ago

Thanks for this. I am no longer maintaining this repo (as stated in the readme), but if you put your changes in a PR I'll merge it.