dongziche8030 2013-03-05 05:03
浏览 130
已采纳

使用PHP Curl与命令行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?

  • 写回答

1条回答 默认 最新

  • douche3791 2013-03-05 19:20
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘