使用PHP Curl与命令行curl的区别

时间:2013-03-05 05:03:03

标签: php api curl

我正在将Badgeville REST API与我的PHP 5.3,curl 7.22应用程序集成。

BV的API文档都使用命令行curl调用作为其示例。当我运行这些例子时,它们工作正常。

当我尝试使用PHP Curl类做同样的事情时,我总是从BV服务器得到500错误。

我尝试使用Chrome中的Advanced Rest Client扩展程序执行同步功能。

PHP Curl示例:

$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客户端示例:

  

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

     

POST   没有标题有效负载:

     

用户[名称] =通用+用户名和安培;用户[电子邮件] =输入johndoe%40domainname.com

我已手动创建了命令行curl调用并使用shell_exec()运行,但我真的不想这样做。

在我的研究中,我发现了一个Drupal module,所有API调用都是通过fsockopen()次调用完成的。

有没有办法成功使用PHP Curl类进行Badgeville调用?

1 个答案:

答案 0 :(得分:1)

事实证明,当卷边请求进入时,Badgeville会出现500错误。

返回代码时出错:

$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);

正常运作的代码:

$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