Patrick
Patrick

Reputation: 3172

Differences in using PHP Curl vs command line curl

I am integrating the Badgeville REST API with my PHP 5.3, curl 7.22 application.

The API documentation for BV all uses command line curl calls for their examples. When I run these examples they work fine.

When I attempt to do the same thing with the PHP Curl class I always get a 500 error from the BV server.

I have tried to do the synonomous functionality with the Advanced Rest Client extension in Chrome.

PHP Curl Example:

$ch = curl_init('http://sandbox.v2.badgeville.com/api/berlin/[private_api_key]/users.json');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);

if($this->getRequestType() == 'POST')
{
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, 
        array(
            'user[name]'    => 'Generic+Username',
            'user[email]'   => 'johndoe%40domainname.com'
        );
    );
}

$response   = curl_exec($ch);

Rest Client Example:

URL: http://sandbox.v2.badgeville.com/api/berlin/[private_api_key]/users.json

POST No Headers Payload:

user[name]=Generic+Username&user[email]=johndoe%40domainname.com

I have manually created the command line curl call and ran that with shell_exec(), but I would REALLY prefer not having to do that.

In my research I found a Drupal module and all the API calls are done through fsockopen() calls.

Is there some way to do successfully make Badgeville calls using the PHP Curl class?

Upvotes: 2

Views: 2759

Answers (1)

Patrick
Patrick

Reputation: 3172

As it turns out Badgeville has a 500 error when a curl request comes in that has headers set.

Error returning code:

$ch = curl_init('http://sandbox.v2.badgeville.com/api/berlin/[private_api_key]/users.json');
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);

if($this->getRequestType() == 'POST')
{
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, 
        array(
            'user[name]'    => 'Generic+Username',
            'user[email]'   => 'johndoe%40domainname.com'
        );
    );
}

$response   = curl_exec($ch);

Properly functioning code:

$ch = curl_init('http://sandbox.v2.badgeville.com/api/berlin/[private_api_key]/users.json');
//curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);

if($this->getRequestType() == 'POST')
{
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, 
        array(
            'user[name]'    => 'Generic+Username',
            'user[email]'   => 'johndoe%40domainname.com'
        );
    );
}

$response   = curl_exec($ch);

SMH

Upvotes: 1

Related Questions