Differences in using PHP Curl vs command line curl

*爱你&永不变心* 提交于 2021-01-27 17:33:31

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/15216206/differences-in-using-php-curl-vs-command-line-curl

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!