cURL error 56: Failure when receiving data from the peer while sending xml request

4.3k Views Asked by At

I was trying to convert curl to use guzzle but I got an error cURL error 56: Failure when receiving data from the peer. Below is what I have so far.

curl_test.php which is working

    $username = "admin";
    $password= "password";
    $request_xml = '<?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE telco_xml_enq SYSTEM "telco_xml_enq.dtd">
      <person>
          <individual>
            <user_id>1234</user_id>
            <full_name>test</full_name>
          </individual>
        </body>
      </person>';
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_USERPWD, $username . ':' . $password);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 4);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_POSTFIELDS, "XML_STRING=" . $request_xml);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: close'));
    $response     = curl_exec($ch);
    return $response;

guzzle_test.php not working

    $request_xml = '<?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE telco_xml_enq SYSTEM "telco_xml_enq.dtd">
      <person>
          <individual>
            <user_id>1234</user_id>
            <full_name>test</full_name>
          </individual>
        </body>
      </person>';
    $client = new Client(["verify"=>false,
                "auth"=>["admin","password"
    ]
    ]);
    $options = [
        "headers"=>
            ["Content-Type"=>"text/xml"],
        "body"=>$requestXml
    ];
    $response = $client->request("POST",$url,$options]);
    $res = $response->getBody();

Can you help me with this?

Any help is greatly appreciated.

Thanks

2

There are 2 best solutions below

0
On

Looks like you have to use XML_STRING for your post values in the body. Due to this receiver is not able to retrieve the values and hence throws an error.

$options = [
    "headers"=>
        ["Content-Type"=>"text/xml"],
    "body" => "XML_STRING=".$requestXml
];
0
On

You need to use form_params instead of body to send a form to the server. Try the code below.

$request_xml = '<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE telco_xml_enq SYSTEM "telco_xml_enq.dtd">
  <person>
      <individual>
        <user_id>1234</user_id>
        <full_name>test</full_name>
      </individual>
    </body>
  </person>';
$client = new Client(["verify"=>false,
            "auth"=>["admin","password"
]
]);
$options = [
    "form_params"=>[
        'XML_STRING' => $requestXml,
    ],
];
$response = $client->request("POST",$url,$options]);
$res = $response->getBody();