Avatar of webfactor
webfactor asked on

Curl request times out php

Hi everyone,
We r trying to make some api calls via curl and our last curl request times out for some reason.

Now we have a small chunk of code that sends an xml file to a seperate system via curl.

As far as we can tell the call code is corect but maybe there s something we r missing in our curl request options so i will paste the code.

Now here s the first part of code, where we just prep the required xml data and make the function call:
	
	$paymentXml='<Payments>
  <Payment>
    <Invoice>
      <InvoiceNumber>'.$invoiceNumber.'</InvoiceNumber>
    </Invoice>
    <Account>
      <AccountID>'.$xeroBankAcc.'</AccountID>
    </Account>
    <Date>'.date("Y-m-d").'T00:00:00</Date>
    <Amount>'.$amt.'</Amount>
  </Payment>
</Payments>';
//echo $paymentXml;
$response = $XeroOAuth->request('PUT', $XeroOAuth->url('Payments', 'core'), array(), $paymentXml);
		  if ($XeroOAuth->response['code'] == 200) {
		    $payment = $XeroOAuth->parseResponse($XeroOAuth->response['response'], $XeroOAuth->response['format']);
		    echo $payments;
		  } else {
		    outputError($XeroOAuth); 
		  }//*/
	}

Open in new window

The second part is from the wrapper library privided to us for using that specific api.
Theres a bit more code here but i think these r all the function that r used when making this call so i ll paste it all here in hope that someone will enlighten us.
 /**
     * Makes a curl request. Takes no parameters as all should have been prepared
     * by the request method
     *
     * @return void response data is stored in the class variable 'response'
     */
    private function curlit()
    {
        // method handling
        switch ($this->method) {
            case 'POST':
                
                break;
            default:
                // GET, DELETE request so convert the parameters to a querystring
                if (!empty($this->request_params)) {
                    foreach ($this->request_params as $k => $v) {
                        // Multipart params haven't been encoded yet.
                        // Not sure why you would do a multipart GET but anyway, here's the support for it
                        if ($this->config['multipart']) {
                            $params[] = $k . '=' . $v;
                        } else {
                            $params[] = $this->safe_encode($k) . '=' . $this->safe_encode($v);
                        }
                    }
                    $qs                   = implode('&', $params);
                    $this->url            = strlen($qs) > 0 ? $this->url . '?' . $qs : $this->url;
                    $this->request_params = array();
                }
                break;
        }
        
        // configure curl
        $c         = curl_init();
        $useragent = (isset($this->config['user_agent'])) ? (empty($this->config['user_agent']) ? 'XeroOAuth-PHP' : $this->config['user_agent']) : 'XeroOAuth-PHP';
        curl_setopt_array($c, array(
            CURLOPT_USERAGENT => $useragent,
            CURLOPT_CONNECTTIMEOUT => $this->config['curl_connecttimeout'],
            CURLOPT_TIMEOUT => $this->config['curl_timeout'],
            CURLOPT_RETURNTRANSFER => TRUE,
            CURLOPT_SSL_VERIFYPEER => $this->config['curl_ssl_verifypeer'],
            CURLOPT_CAINFO => $this->config['curl_cainfo'],
            CURLOPT_SSL_VERIFYHOST => $this->config['curl_ssl_verifyhost'],
            CURLOPT_FOLLOWLOCATION => $this->config['curl_followlocation'],
            CURLOPT_PROXY => $this->config['curl_proxy'],
            CURLOPT_ENCODING => $this->config['curl_encoding'],
            CURLOPT_URL => $this->sign['signed_url'],
            CURLOPT_VERBOSE => $this->config['curl_verbose'],
            // process the headers
            CURLOPT_HEADERFUNCTION => array(
                $this,
                'curlHeader'
            ),
            CURLOPT_HEADER => FALSE,
            CURLINFO_HEADER_OUT => TRUE
        ));
        
        if ($this->config['application_type'] == "Partner") {
            curl_setopt_array($c, array(
                // ssl client cert options for partner apps
                CURLOPT_SSLCERT => $this->config['curl_ssl_cert'],
                CURLOPT_SSLKEYPASSWD => $this->config['curl_ssl_password'],
                CURLOPT_SSLKEY => $this->config['curl_ssl_key']
            ));
        }
        
        if ($this->config['curl_proxyuserpwd'] !== false)
            curl_setopt($c, CURLOPT_PROXYUSERPWD, $this->config['curl_proxyuserpwd']);
        
        if (isset($this->config['is_streaming'])) {
            // process the body
            $this->response['content-length'] = 0;
            curl_setopt($c, CURLOPT_TIMEOUT, 0);
            curl_setopt($c, CURLOPT_WRITEFUNCTION, array(
                $this,
                'curlWrite'
            ));
        }
        
        switch ($this->method) {
            case 'GET':
            	$contentLength = 0;
                break;
            case 'POST':
                curl_setopt($c, CURLOPT_POST, TRUE);
                $post_body = $this->safe_encode($this->xml);
        		curl_setopt($c, CURLOPT_POSTFIELDS, $post_body);
        		$this->request_params['xml'] = $post_body;
        		$contentLength = strlen($post_body);
                
                break;
            case 'PUT':
                $fh = fopen('php://memory', 'w+');
                $put_body = $this->safe_encode($this->xml);
                fwrite($fh, $put_body);
                rewind($fh);
                curl_setopt($c, CURLOPT_PUT, true);
                curl_setopt($c, CURLOPT_INFILE, $fh);
                curl_setopt($c, CURLOPT_INFILESIZE, strlen($put_body));
                $contentLength = strlen($put_body);
                
                break;
            default:
                curl_setopt($c, CURLOPT_CUSTOMREQUEST, $this->method);
        }
        
        if (!empty($this->request_params)) {
            // if not doing multipart we need to implode the parameters
            if (!$this->config['multipart']) {
                foreach ($this->request_params as $k => $v) {
                    $ps[] = "{$k}={$v}";
                }
                $this->request_params = implode('&', $ps);
            }
            curl_setopt($c, CURLOPT_POSTFIELDS, $this->request_params);
        } else {
            // CURL will set length to -1 when there is no data
            $this->headers['Content-Type']   = '';
            $this->headers['Content-Length'] = $contentLength;
        }
        
        $this->headers['Expect'] = '';
        
        if (!empty($this->headers)) {
            foreach ($this->headers as $k => $v) {
                $headers[] = trim($k . ': ' . $v);
            }
            curl_setopt($c, CURLOPT_HTTPHEADER, $headers);
        }
        
        if (isset($this->config['prevent_request']) && false == $this->config['prevent_request'])
            return;
        
        
        // do it!
        $response = curl_exec($c);
        if ($response === false) {
            $response = 'Curl error: ' . curl_error($c);
            $code     = 1;
        } else {
            $code = curl_getinfo($c, CURLINFO_HTTP_CODE);
        }
        
        $info = curl_getinfo($c);
        
        curl_close($c);
        
        // store the response
        $this->response['code']     = $code;
        $this->response['response'] = $response;
        $this->response['info']   = $info;
        $this->response['format'] = $this->format;
        return $code;
    }
    
    function MakeRequest($endpoint, $parameters, $action, $data, $app_type, $format = "xml")
    {
        $oauthObject = new OAuthSimple();
        
        # Set some standard curl options....
        
        $useragent                       = USER_AGENT;
        $useragent                       = isset($useragent) ? USER_AGENT : 'XeroOAuth-PHP';
        $options[CURLOPT_USERAGENT]      = $useragent;
        $options[CURLOPT_VERBOSE]        = 1;
        $options[CURLOPT_RETURNTRANSFER] = 1;
        $options[CURLOPT_SSL_VERIFYHOST] = 0;
        $options[CURLOPT_SSL_VERIFYPEER] = 0;
        
    }
    
    /**
     * Make an HTTP request using this library. This method doesn't return anything.
     * Instead the response should be inspected directly.
     *
     * @param string $method the HTTP method being used. e.g. POST, GET, HEAD etc
     * @param string $url the request URL without query string parameters
     * @param array  $params the request parameters as an array of key=value pairs
     * @param string $format the format of the response. Default json. Set to an empty string to exclude the format
     
     */
    function request($method, $url, $params = array(), $xml = "", $format = 'xml')
    {
    	// removed these as function parameters for now
    	$useauth = true; $multipart = false;
        
        if (isset($format)) {
            switch ($format) {
                case "pdf":
                    $this->headers['Accept'] = 'application/pdf';
                    break;
                case "json":
                    $this->headers['Accept'] = 'application/json';
                    break;
                case "xml":
                default:
                    $this->headers['Accept'] = 'application/xml';
                    break;
            }
        }
        
        if ($xml !== "")
            $this->xml = $xml;
            
        if($method == "POST")
        	$params['xml'] = $xml;
        
        $this->prepare_method($method);
        $this->config['multipart'] = $multipart;
        $this->url                 = $url;
        if (!isset($_REQUEST['order']))
            $_REQUEST['order'] = "";
        $oauthObject = new OAuthSimple();
        try {
            $this->sign = $oauthObject->sign(array(
                'path' => $url,
                'action' => $method,
                'parameters' => array_merge($params, array(
                    'order' => urlencode($_REQUEST['order']),
                    'oauth_signature_method' => $this->config['signature_method']
                )),
                'signatures' => $this->config
            ));
        }
        
        catch (Exception $e) {
            $errorMessage = $e->getMessage();
        }
        $this->format = $format;
        
        $curlRequest = $this->curlit();
        
        if ($this->response['code'] == 401 && isset($this->config['session_handle'])) {
            if ((strpos($this->response['response'], "oauth_problem=token_expired") !== false)) {
                $this->response['helper'] = "TokenExpired";
				
            } else {
                $this->response['helper'] = "TokenFatal";
            }
            
        }
        if ($this->response['code'] == 403) {
            $errorMessage               = "It looks like your Xero Entrust cert issued by Xero is either invalid or has expired. See http://developer.xero.com/api-overview/http-response-codes/#403 for more";
            // default IIS page isn't informative, a little swap
            $this->response['response'] = $errorMessage;
            $this->response['helper']   = "SetupIssue";
        }
        if ($this->response['code'] == 0) {
            $errorMessage               = "It looks like your Xero Entrust cert issued by Xero is either invalid or has expired. See http://developer.xero.com/api-overview/http-response-codes/#403 for more";
            $this->response['response'] = $errorMessage;
            $this->response['helper']   = "SetupIssue";
        }
        
        
        return $this->response;
    }

Open in new window


Hope to hear from someone son and thank you for your help
PHPScripting LanguagesSQL

Avatar of undefined
Last Comment
webfactor

8/22/2022 - Mon
SOLUTION
Kevin Cross

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
SOLUTION
Ray Paseur

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER CERTIFIED SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER
webfactor

Unfortunately i don't have the details of the end result
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy