首页 > 解决方案 > 从 Phalcon 网站到 Prestashop 网络服务的 POST 和 PUT 请求不起作用

问题描述

一段时间以来,我在尝试从另一个使用 PHP 框架 Phalcon 开发的网站向 Prestashop Webservice 发送 POST 或 PUT 请求时遇到了一个问题。以下是我用于按照此示例https://github.com/phalcon/incubator/tree/master/Library/Phalcon/Http/Client初始化我的提供程序的代码:

$uriCustomer = $this->config->application->storeAPIUrl;

       // get available provider Curl or Stream
       try {
           $provider = Request::getProvider();
       } catch (Exception $e) {
       }

       $provider->header->set('Content-Type', 'application/xml');
       $provider->header->set('Accept', '*/*');

这是我用来发送请求的代码:

$response = $provider->post(
            $uriCustomer.'/customers',
            [
                'body'        => $xml
            ]
        );

        var_dump($xml);
        dump($response->body);exit();

这是我尝试发送的两个xml(第一个是我执行get请求时由api返回的):

$xmlBlank = '<prestashop xmlns:xlink="http://www.w3.org/1999/xlink">
<customer>
<id_default_group xlink:href="http://local.loveinlive-store.com/api/groups/3"><![CDATA[3]]></id_default_group>
<id_lang xlink:href="http://local.loveinlive-store.com/api/languages/1"><![CDATA[1]]></id_lang>
<newsletter_date_add><![CDATA[0000-00-00 00:00:00]]></newsletter_date_add>
<ip_registration_newsletter></ip_registration_newsletter>
<last_passwd_gen><![CDATA[2019-12-11 10:09:00]]></last_passwd_gen>
<secure_key><![CDATA[7edcb15d69ef0bf455e706857d63bf97]]></secure_key>
<deleted><![CDATA[0]]></deleted>
<passwd><![CDATA[$fjezhngfgr]]></passwd>
<lastname><![CDATA[weggs]]></lastname>
<firstname><![CDATA[loic]]></firstname>
<email><![CDATA[loic@weggs.fr]]></email>
<id_gender><![CDATA[1]]></id_gender>
<birthday><![CDATA[0000-00-00]]></birthday>
<newsletter><![CDATA[0]]></newsletter>
<optin><![CDATA[0]]></optin>
<website></website>
<company></company>
<siret></siret>
<ape></ape>
<outstanding_allow_amount><![CDATA[0.000000]]></outstanding_allow_amount>
<show_public_prices><![CDATA[0]]></show_public_prices>
<id_risk><![CDATA[0]]></id_risk>
<max_payment_days><![CDATA[0]]></max_payment_days>
<active><![CDATA[1]]></active>
<note></note>
<is_guest><![CDATA[0]]></is_guest>
<id_shop><![CDATA[1]]></id_shop>
<id_shop_group><![CDATA[1]]></id_shop_group>
<date_add><![CDATA[2019-12-11 16:09:00]]></date_add>
<date_upd><![CDATA[2019-12-11 16:09:00]]></date_upd>
<reset_password_token></reset_password_token>
<reset_password_validity><![CDATA[0000-00-00 00:00:00]]></reset_password_validity>
<associations>
<groups nodeType="group" api="groups">
<group xlink:href="http://local.loveinlive-store.com/api/groups/3">
<id><![CDATA[3]]></id>
</group>
</groups>
</associations>
</customer>
</prestashop>';
$xml ='<prestashop xmlns:xlink="http://www.w3.org/1999/xlink">
            <customer>
                <id_default_group xlink:href="http://local.loveinlive-store.com/api/groups/3">3</id_default_group>
                <id_lang xlink:href="http://local.loveinlive-store.com/api/languages/1">1</id_lang>
                <newsletter_date_add></newsletter_date_add>
                <ip_registration_newsletter></ip_registration_newsletter>
                <last_passwd_gen></last_passwd_gen>
                <secure_key></secure_key>
                <deleted></deleted>
                <passwd>testtest</passwd>
                <lastname>test</lastname>
                <firstname>test</firstname>
                <email>test@test.fr</email>
                <id_gender>1</id_gender>
                <birthday></birthday>
                <newsletter></newsletter>
                <optin></optin>
                <website></website>
                <company></company>
                <siret></siret>
                <ape></ape>
                <outstanding_allow_amount></outstanding_allow_amount>
                <show_public_prices></show_public_prices>
                <id_risk></id_risk>
                <max_payment_days></max_payment_days>
                <active>1</active>
                <note></note>
                <is_guest></is_guest>
                <id_shop>1</id_shop>
                <id_shop_group>1</id_shop_group>
                <date_add></date_add>
                <date_upd></date_upd>
                <reset_password_token></reset_password_token>
                <reset_password_validity></reset_password_validity>
                <associations>
                    <groups nodeType="group" api="groups">
                        <group xlink:href="http://local.loveinlive-store.com/api/groups/3">
                            <id>3</id>
                        </group>
                    </groups>
                </associations>
            </customer>
        </prestashop>';

该请求在使用 Postman 时完美运行,但在我的应用程序中,我在转储响应时不断收到此错误:

<?xml version="1.0" encoding="UTF-8"?>
<prestashop xmlns:xlink="http://www.w3.org/1999/xlink">
<errors>
<error>
<code><![CDATA[3]]></code>
<message><![CDATA[[PHP Warning #2] SimpleXMLElement::__construct(): Entity: line 1: parser error : Start tag expected, '<' not found (/home/loic/web/loveinlives ▶
</error>
<error>
<code><![CDATA[3]]></code>
<message><![CDATA[[PHP Warning #2] SimpleXMLElement::__construct(): %3Cprestashop+xmlns%3Axlink%3D%22http%3A%2F%2Fwww.w3.org%2F1999%2Fxlink%22%3E%3C (/home/loic ▶
</error>
<error>
<code><![CDATA[3]]></code>
<message><![CDATA[[PHP Warning #2] SimpleXMLElement::__construct(): ^ (/home/loic/web/loveinlivestore/classes/webservice/WebserviceRequest.php, line 1497)]]></m ▶
</error>
<error>
<code><![CDATA[127]]></code>
<message><![CDATA[XML error : String could not be parsed as XML
XML length : 1727
Original XML : %3Cprestashop+xmlns%3Axlink%3D%22http%3A%2F%2Fwww.w3.org%2F1999%2Fxlink%22%3E%3Ccustomer%3E%3Cid_default_group+xlink%3Ahref%3D%22http%3A%2F%2Floc ▶
</error>
</errors>
</prestashop>

我知道已经报告了这类问题,但提到的解决方案都没有解决我的问题,所以欢迎任何帮助

编辑:我终于成功使它工作。我按照这里显示的代码https://github.com/phalcon/incubator/tree/master/Library/Phalcon/Http/Client。但实际上,请求的主体不应该传入参数数组。因此,更正只是改变:

$response = $provider->post(
            $uriCustomer.'/customers',
            [
                'body'        => $xml
            ]
        );

为了

$response = $provider->post($uriCustomer.'/customers',
            $xml
        );

最后给出了以下方法:

public function postCustomerPersonalDatas($user) {

        // Uri of Prestashop Api formatted like http://API_KEY@prestashopUrl.com/api
        $uriCustomer = $this->config->application->storeAPIUrl;

        // get available provider Curl or Stream
        try {
            $provider = Request::getProvider();
        } catch (Exception $e) {
            throwException($e);
        }

        // Return a string containing a formatted customer that you can then fill as you want
        $customerModel = $provider->get($uriCustomer.'/customers?schema=blank'
        );

//        Turn the returned string into XML so you can easily edit it
        $customerModel = new \SimpleXMLElement($customerModel->body);

//       Call a function that return the datas I need
        $customerDatas = $this->getCustomerPersonalDatas($user);

//      Edit the formatted xml with wanted datas
        $customerXML = $customerModel;
        $customerXML->customer[0]->id_customer_lil = $customerDatas['id_customer_lil'];
        $customerXML->customer[0]->passwd = $customerDatas['passwd'];
        $customerXML->customer[0]->email = $customerDatas['email'];
        $customerXML->customer[0]->id_gender = $customerDatas['id_gender'];
        $customerXML->customer[0]->birthday = $customerDatas['birthday'];
        $customerXML->customer[0]->lastname = strval($customerDatas['id_customer_lil']).'lastnamelil';
        $customerXML->customer[0]->firstname = strval($customerDatas['id_customer_lil']).'firstnamelil';

//        Send the request using asXML() function as Prestashop webservice expect to receive a string
        try{
            $response = $provider->post($uriCustomer.'/customers',
                $customerXML->asXML()
            );
            return 'Success on store registration';
        } catch (\Phalcon\Exception $e) {
            return $e;
        }
    }

标签: phpxmlprestashopphalcon

解决方案


推荐阅读