首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么这个cURL请求可以在命令行上工作,而不能在PHP中工作?

为什么这个cURL请求可以在命令行上工作,而不能在PHP中工作?
EN

Stack Overflow用户
提问于 2012-04-14 06:04:15
回答 2查看 2K关注 0票数 0

我为Highrise的API编写了一个非常基本的包装类。它对于读取(GET)非常有效,而我刚刚开始测试它用于创建(POST)。据我所知,这两个请求(一个在命令行上,一个通过PHP的cURL库)是相同的。同样的XML,相同的选项set....just一个能用,另一个不行。

任何帮助都是非常感谢的。我也把这个问题贴到了37signals开发者邮件列表上,但stackoverflow在发现我的愚蠢错误时通常更快……

这是我在PHP的cURL中得到的错误(这让我认为Highrise在解析XML字符串时遇到了问题):

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?> <errors> <error>First name can't be blank</error> </errors> 

以下是在命令行上的工作方式:

代码语言:javascript
复制
curl -u 'my_api_key:X'
    -H 'Content-type: application/xml'
    -d '<?xml version="1.0" encoding="UTF-8"?> <person><first-name>Savos</first-name><last-name>Aren</last-name><title>Archmage</title><company-name>Winterhold College</company-name><contact-data><email-addresses/><phone-numbers><phone-number><number>555-555-5555</number><location>Home</location></phone-number><phone-number><number>555-555-5555</number><location>Work</location></phone-number><phone-number><number>555-555-5555</number><location>Mobile</location></phone-number></phone-numbers><addresses><address><city>Winterhold</city><country>Tamriel</country><state>Skyrim</state><street>Hall of the Elements, College of Winterhold</street><zip>99999</zip><location>Work</location></address></addresses></contact-data></person>'
    https://myuserid.highrisehq.com/people.xml

下面是我的包装器类:

代码语言:javascript
复制
class HighriseAPICall {
    protected $_timeout = 120;
    protected $_url_prefix = 'https://';
    protected $_url_suffix = '.highrisehq.com';
    protected $_password = 'X';

    protected $_userpwd;
    protected $_url;

    public function __construct($api_key, $username) {
        $this->_userpwd= $api_key . ':' . $this->_password;
        $this->_url = $this->_url_prefix . $username . $this->_url_suffix;
    }

    /**
     * Make the specified API call.
     * @param string $action one of the four HTTP verbs supported by Highrise
     * @param string $resource_name the Highrise resource to be accessed
     * @param string $xml a well-formed XML string for a Highrise create, update, or delete request
     * 
     * $xml parameter should include any query parameters as suggested by Highrise API documentation
     * eg, if you want to GET all People, pass in "/people.xml"
     * and if you want to get People by search term where field=value,
     * then pass in "/people/search.xml?criteria[field]=value"
     */
    public function makeAPICall($action,$resource_name,$xml=null) {
        /* initialize curl session and set defaults for new API call */
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $this->_url . $resource_name);
        curl_setopt($curl, CURLOPT_USERPWD, $this->_userpwd);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
        curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $this->_timeout);
        /* if xml was passed in, set header and postfields */
        if (isset($xml)) {
            curl_setopt($curl, CURLOPT_HTTPHEADER, 'Content-type: application/xml');
            curl_setopt($curl, CURLOPT_POSTFIELDS, "$xml");
        }
        /* set action as custom request */
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $action);
        /* get the string response from executing the curl session */
        $result = curl_exec($curl);
        curl_close($curl);

        // return the response as a simpleXMLElement
        try {
                $result_simplexml = new SimpleXMLElement($result);
        }
        catch (Exception $e) {
                throw new Exception("Highrise API Call Error: " . $e->getMessage() . ", Response: " . $result);
        }
        if (!is_object($result_simplexml)) {
                throw new Exception("Highrise API Call Error: Could not parse XML, Response: " . $result);
        }
        return $result_simplexml;
    }

}
?>

和我正在使用的简单测试页面:

代码语言:javascript
复制
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
    </head>
    <body>
        <?php
            require_once('HighriseAPICall.class.php');
            $highrise_api_key = 'OBSCURED';
            $highrise_username = 'OBSCURED';
            $highrise_api = new HighriseAPICall($highrise_api_key, $highrise_username);

            $person_xml ='<?xml version="1.0" encoding="UTF-8"?> <person><first-name>Savos</first-name><last-name>Aren</last-name><title>Archmage</title><company-name>Winterhold College</company-name><contact-data><email-addresses/><phone-numbers><phone-number><number>555-555-5555</number><location>Home</location></phone-number><phone-number><number>555-555-5555</number><location>Work</location></phone-number><phone-number><number>555-555-5555</number><location>Mobile</location></phone-number></phone-numbers><addresses><address><city>Winterhold</city><country>Tamriel</country><state>Skyrim</state><street>Hall of the Elements, College of Winterhold</street><zip>99999</zip><location>Work</location></address></addresses></contact-data></person>';

            $response = $highrise_api->makeAPICall('POST', '/people.xml', $person_xml);
            echo htmlentities($response->asXML());
        ?>
    </body>
</html>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-04-17 01:10:32

在我的包装器类中,有一行:

代码语言:javascript
复制
curl_setopt($curl, CURLOPT_HTTPHEADER, 'Content-type: application/xml');

应该是:

代码语言:javascript
复制
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-type: application/xml'));
票数 1
EN

Stack Overflow用户

发布于 2012-04-14 06:10:32

尝试这几行代码,而不是脚本中的代码

代码语言:javascript
复制
   if (isset($xml)) {
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-type: application/xml'));
        curl_setopt($curl, CURLOPT_POSTFIELDS, $xml);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER,false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST ,false);
    }
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10148895

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档