1

我从几天开始就面临这个问题,但我仍然无法在网上或我之前的问题中找到任何答案或解决方案(实际上有一些错误)。

就是这样的情景。我必须使用 gls webservice 来添加包裹或列出插入的包裹。我正在使用 curl 构建客户端并进行调用,这是该类的代码:

/*Headers*/
public function buildGlsHeaders($glsCall,$gls_lenght,$soap_action)
{       
    //header soap 1.1
    $headers = array(
        "Content-Type: text/xml; charset=utf-8",
        "Content-Length: " . $gls_lenght,
        $soap_action,
        );

    return $headers;
}

/*Request*/
public function sendRequest($glsCall, $requestBody, $gls_lenght, $soap_action)
{
    $cookiePath = tempnam('/tmp', 'cookie');

    //build gls headers using variables passed via constructor as well as the gls call to use
    $headers = $this->buildGlsHeaders($glsCall, $gls_lenght, $soap_action);

    //initialise a CURL session
    $connection = curl_init();

    //set the server we are using 
    curl_setopt($connection, CURLOPT_URL, 'http://weblabeling.gls-italy.com/IlsWebService.asmx');

    //Time out
    curl_setopt($connection, CURLOPT_CONNECTTIMEOUT, 10);
    curl_setopt($connection, CURLOPT_TIMEOUT, 10);

    //set it to return the transfer as a string from curl_exec
    curl_setopt($connection, CURLOPT_RETURNTRANSFER, true);

    //stop CURL from verifying the peer's certificate
    curl_setopt($connection, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($connection, CURLOPT_SSL_VERIFYHOST, false);

    //set method as POST
    curl_setopt($connection, CURLOPT_POST, true);

    //set the XML body of the request
    curl_setopt($connection, CURLOPT_POSTFIELDS, $requestBody);

    //set the headers using the array of headers
    curl_setopt($connection, CURLOPT_HTTPHEADER, $headers);

    //Header
    curl_setopt($connection, CURLINFO_HEADER_OUT, true);
    curl_setopt($connection, CURLOPT_HEADER, true);

    curl_setopt($connection, CURLOPT_COOKIEJAR, $cookiePath);

    //Send the Request
    $response = curl_exec($connection);

    print_r(curl_getinfo($connection));

    echo "\n" . 'Header Code: ' . curl_getinfo($connection, CURLINFO_HTTP_CODE) . "\n\n";

    //close the connection
    curl_close($connection); 

    //return the response
    return $response;
}

变量 $glsCall、$requestBody、$gls_lenght、$soap_action 来自脚本本身:

$soap_action = "SOAPAction: \"http://weblabeling.gls-italy.com/AddParcel\"";

$gls_lenght = strlen($xml); 

并且请求由以下行发送:

/*AddParcel*/
$glsResponse = $gls->sendRequest('AddParcel', $xml, $gls_lenght, $soap_action);

/*ListSped*/
$glsResponse = $gls->sendRequest('ListSped', $xml, $gls_lenght, $soap_action);

现在两个调用都以相同的方式构造,但其中一个是嵌套的:

$Label = array(
                'XMLInfoParcel' => array(
                'Info' => array(
                    'SedeGls' => $SedeGls,
                    'CodiceClienteGls' => $CodiceClienteGls,
                    'PasswordClienteGls' => $PasswordClienteGls,                
                    'Parcel' => array(
                                       'CodiceContrattoGls' => $cod_cont,
                                       'RagioneSociale' => $rag_soc,
                                       'Indirizzo' => $delivery_indirizzo,
                                       'Localita' => $delivery_city,
                                       'Zipcode' => $data['delivery_postcode'],
                                       'Provincia' => $data['zone_code'],
                                       'Bda' => '',
                                       'DataDocumentoTrasporto' => '',
                                       'Colli' => '1',
                                       'Incoterm' => '',
                                       'PesoReale' => '1,00',
                                       'ImportoContrassegno' => $imp_cont,
                                       'NoteSpedizione' => $data['customers_telephone'],
                                       'TipoPorto' => 'F',
                                       'Assicurazione' => $ass_ins,
                                       'PesoVolume' => '',
                                       'TipoCollo' => $tipo_collo,
                                       'FrancoAnticipata' => '',
                                       'RiferimentoCliente' => '',
                                       'NoteAggiuntive' => '',
                                       'CodiceClienteDestinatario' => '',
                                       'Email' => '',
                                       'Cellulare1' => $telefono,
                                       'Cellulare2' => '',
                                       'ServiziAccessori' => '',
                                       'ModalitaIncasso' => $mod_inc    
                                  ),),),                                
                );

而另一个不是:

/*Request*/
    $listsp = array(
                    'SedeGls' => $SedeGls,
                    'CodiceClienteGls' => $CodiceClienteGls,
                    'PasswordClienteGls' => $PasswordClienteGls 
                    );

这些是请求输入:

添加包裹

在此处输入图像描述

列出速度

在此处输入图像描述

如您所见,输入格式不同,这是 AddParcel "string" 的架构

在此处输入图像描述

它继续与其他领域并关闭

       </Parcel>
  </Info>

ListSped 调用完美地工作,而 AddParcel 没有,它总是返回 400 Bad 请求标头。我认为 xml 是正确的,因为我将它发送给 GLS IT 支持,并且他们确认我在直接上传时它可以工作。不幸的是,他们不支持 php。

我正在考虑导致问题的嵌套数组,但我觉得不可能,因为如果我更改 xml 结构 webservice 答案正确:“xml 结构错误”。

我一直在环顾四周,我发现其他一些人有同样的问题,但没有找到解决方案。我想让脚本在 php 中工作,而不是使用其他我不知道的语言。

4

1 回答 1

1

正如我们在请求输入的图片“AddParcel”中看到的,输入本身是一个字符串。

我意识到它希望将其格式化为:

    &lt;Info&gt;&lt;SedeGls&gt;XXX&lt;/SedeGls&gt;&lt;CodiceContrattoGls&gt;XXX&lt;/CodiceContrattoGls&gt; <!--and so on-->
于 2012-11-06T14:00:11.100 回答