[PHP] Curl traag

Pagina: 1
Acties:

Onderwerpen


Acties:
  • 0 Henk 'm!

Verwijderd

Topicstarter
Hoi,

Kzit met een probleempje met een Curlscript voor een XMLwebservice van een van onze leveranciers.
Op de testserver draait alles vlot (curl versie: libcurl/7.10.4 OpenSSL/0.9.6b zlib/1.1.4) maar op de live server (curl versie: libcurl/7.14.0 OpenSSL/0.9.7b zlib/1.1.4) duurt het makkelijk een 10tal minuten voor de eerste 20 items opgevraagd zijn bij de webservice. Ik heb een class geschreven voor het oproepen van de service. Hier de class en de code die gebruikt wordt voor het aanroepen van de class.

PHP:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
/**
* Soap handler
*
* @author    Chris Ramakers <chris@boekensoft.com>
* @date      14/07/2005
* @REQUIRES CURL ENABLED PHP INSTALLATION!
*
*/

class imHandler {
    
    var $cc; // Curl connection
    var $server; // Server to connect to
    var $xml; // The XML request with all data
    var $loginID; // IM loginID
    var $password; // IM Pwd
    var $countryCode = "BE"; // 2 Letter international country code

    /**
    * Constructor
    **/
    function imHandler($server="https://im-xml.com/receivexml.asp", $login = "xxxx", $pass = "xxxx") {
        if (isset($server)) $this->setServer($server);
        if (isset($login))  $this->setLoginID($login);
        if (isset($pass))   $this->setPassword($pass);
        $this->cc = curl_init(); // init curl connection
        $res = curl_setopt ($this->cc, CURLOPT_URL, $this->server);
        $res = curl_setopt ($this->cc, CURLOPT_HEADER, 0); /// Header control
        $res = curl_setopt ($this->cc, CURLOPT_POST, 1); /// tell it to make a POST, not a GET
        $res = curl_setopt ($this->cc, CURLOPT_RETURNTRANSFER, 1); /// This allows the output to be set into a variable $xyz
    }
    
    /**
    * Set & Get Server
    **/
    function setServer($newServer) {
        $this->server = $newServer;
    }
    function getServer() {
        return $this->server;
    }

    /**
    * Set & Get Country code
    **/
    function setCountryCode($newCountryCode) {
        $this->countryCode = $newCountryCode;
    }
    function getCountryCode() {
        return $this->countryCode;
    }

    /**
    * Set & Get LoginId
    **/
    function setLoginID($newLoginID) {
        $this->loginID = $newLoginID;
    }
    function getLoginID() {
        return $this->loginID;
    }

    /**
    * Set & Get password
    **/
    function setPassword($newPassword) {
        $this->password = $newPassword;
    }
    function getPassword() {
        return $this->password;
    }

    /**
    * Construct Transaction Header
    **/
    function makeHeader() {
        $buffer = ""; // Empty buffer
        $buffer = "
        <TransactionHeader>
            <SenderID>123456789</SenderID>
            <ReceiverID>987654321</ReceiverID>
            <CountryCode>".$this->getCountryCode()."</CountryCode>
            <LoginID>".$this->getLoginID()."</LoginID>
            <Password>".$this->getPassword()."</Password>
        </TransactionHeader>
        ";
        return $buffer;
    }
    
    /**
    * Construct PNA Lines
    **/
    function makePnaLines($lines = array()) {
        $buffer = ""; // Empty buffer
        foreach ($lines as $key=>$line) {
            $buffer .= '<PNAInformation SKU="'.$line["sku"].'" Quantity="'.$line["qty"].'"/>'."\r\n";
        }
        return $buffer;
    }
    
    /**
    * Construct the request in XML
    **/
    function makeXML($data = array()) {
        $this->xml = "
        <PNARequest>
            <Version>2.0</Version>
            ".$this->makeHeader()."
            ".$this->makePnaLines($data)." 
        </PNARequest>
        ";
    }
    
    /**
    * execute the request and return the XML data
    **/
    function executeRequest() {
        curl_setopt ($this->cc, CURLOPT_POSTFIELDS, $this->xml); /// put the query string here starting with "?"
        $reply = curl_exec ($this->cc); /// execute the curl session and return the output to a variable $reply
        return $reply;
    }
    
    /**
    * Close Curl Extension
    **/
    function curlClose() {
        curl_close($this->cc);
    }

}

?>



<?php
// Ik kan max 20 items tegelijk opvragen, daarom splits ik de array in delen van 20
// En doorloop ik x maal onderstaande procedure
foreach ($the_request as $request) {
    $myCurl = new imHandler();
    $myCurl->makeXML($request);
    $requestReturn[$i] = $myCurl->executeRequest();
    echo "Processed part ".($i+1)." of ".$total."\n";
    $i++;
    $myCurl->curlClose();
    unset($myCurl);
}


// Hierna verwerk ik gewoon de array $requestReturn met een XMLparser
?>



Iemand enig idee waarom dit zo traag loopt op de server met curl versie libcurl/7.14.0 OpenSSL/0.9.7b zlib/1.1.4???

Acties:
  • 0 Henk 'm!

  • cerberusss
  • Registratie: Augustus 2005
  • Laatst online: 13-05-2021
Je zou eens moeten kijken waarin de vertraging zit; run vanaf die productieserver eens met de hand een paar curl-sessies. Duurt het resolven van het adres te lang? Gebruik eens direct een IP adres? Heeft die productiedoos geen netwerkproblemen? Probeer eens een andere webservice.

Acties:
  • 0 Henk 'm!

  • Bosmonster
  • Registratie: Juni 2001
  • Laatst online: 18-09 16:28

Bosmonster

*zucht*

Uitaard moet je even kijken waar de bottleneck zit, zoals cerberusss zegt. Maar probeer ook eens de volgende regels:

curl_setopt ($this->cc, CURLOPT_ENCODING , "gzip");
curl_setopt ($this->cc, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt ($this->cc, CURLOPT_SSL_VERIFYHOST, false);

Oftewel, gebruik compressie, en disable de SSL verificaties. En kijk of het enige verbetering oplevert.

Het adres https://im-xml.com/receivexml.asp invoeren in de browser gaat allemaal heel rap overigens.. dus kan me bijna niet voorstellen dat lookup een probleem is.

Controleer ook na je curl_exec() eens op een curl_error(). Sowieso geen slecht idee om error handling in te bouwen natuurlijk in het geval dat de server down is bijvoorbeeld :)

[ Voor 20% gewijzigd door Bosmonster op 26-08-2005 13:27 ]