0

I am working on third party api and need to post data in curl with "xml http headers".I had tried it with many ways but api does not return any error or any response.It returns empty response.This is my code

$url = 'https://google.com'; // example url
    $fields=array( 
        'apikey' =>'xxxxxxxxxxxxxxxxxx',
        'totalItemAmount' => '70.00',
        'totalPaymentAmount' =>'70.00',
        'OrderNo' => '1234',
        'PhoneNo' => '1823434423',
        'addressCode' => 'S',
        'lastName' => 'Johnson',
        'firstName' =>'Thomas',
        'addressLine1' => '345 D lake',
        'city' =>'Chicago',
        'stateCode' => 'IL',
        'zipCode' =>'60579',
        'countryCode' => 'US',
        'itemQty' =>'2'
);
    $fields_string = '';
    //url-ify the data for the POST
    foreach($fields as $key=>$value) 
    { 
        $fields_string .= $key.'='.$value.'&'; 
    }
    rtrim($fields_string, '&');
    //open connection

    $ch = curl_init();
    curl_setopt_array($ch, array(
    CURLOPT_HTTPHEADER  => array('Content-Type: application/xml', 'Accept: application/xml'),
    CURLOPT_RETURNTRANSFER  =>true,
    CURLOPT_SSL_VERIFYPEER  => false,
    CURLOPT_URL => $url,
    CURLOPT_POST => count($fields),
    CURLOPT_POSTFIELDS => http_build_query($fields) 
));
    //execute post
    $result = curl_exec($ch);
    print_r($result);
    //close connection 
    curl_close($ch);
testingexpert
  • 53
  • 1
  • 9
  • Sending a standard `key=value` style POST request with an XML header doesn't make much sense. You probably need to format your attributes as XML - the owner of the API is the only one that can help with how this will need to be formatted. – iainn Aug 24 '17 at 10:22
  • https://stackoverflow.com/questions/4331545/php-https-post-xml-data-with-curl – Naincy Aug 24 '17 at 10:33
  • `but api does not return any error or any response` wrong, it does. most likely, it returns a HTTP 400 Bad Request header response. but you provide no code to view the HEADER responses, so you think `it doesn't return anything`. enable CURLOPT_VERBOSE, then you'll see what response the api really returns. also, you say to the server that you send the data XML-encoded, but your code actually sends it `multipart/form-data`-encoding, that should not work at all (and apparently, doesn't). if you need to xml encode it, maybe check out https://stackoverflow.com/a/43697765/1067003 – hanshenrik Aug 24 '17 at 11:53

0 Answers0