使用PHP Curl与命令行curl的区别 [英] Differences in using PHP Curl vs command line curl

查看:114
本文介绍了使用PHP Curl与命令行curl的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

当我尝试对PHP Curl类执行相同的操作时,我总是从BV服务器收到500错误.

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

我试图通过Chrome中的Advanced Rest Client扩展来实现同步功能.

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

PHP Curl示例:

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

其他客户端示例:

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

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

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

我已经手动创建了命令行curl调用,并使用 shell_exec()运行了该调用,但是我真的希望不必这样做.

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.

在我的研究中,我发现了一个Drupal 模块,并且所有API调用都是通过 fsockopen()调用完成的.

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

是否可以使用PHP Curl类成功完成Badgeville调用?

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

推荐答案

事实证明,当传入设置了标头的curl请求时,Badgeville出现500错误.

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

错误返回代码:

$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

这篇关于使用PHP Curl与命令行curl的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆