简体   繁体   中英

PHP Curl request to IIS results in request format is invalid

I am trying to use curl to access 3rd party webservice, I used the following code which works well if I try it on my own linux server, the data is being sent ok, but the IIS on the 3rd party server returns an error.

$longdata is a long string of data, maybe over 1000 characters long

the 3rd party has many working clients with various implementations so the problem is on my side.

what do I need to add to the request in order to get this through ?

<?php

    $c = curl_init();

//  curl_setopt($c, CURLOPT_HTTPHEADER, array('Expect:'));
    curl_setopt($c, CURLOPT_URL, 'http://XXX.com/test/index.asmx');        
    curl_setopt($c, CURLOPT_POST, 1);
    curl_setopt($c, CURLOPT_HEADER, 1);
    curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);

    $post = array('param1' => 'XXXX', "param2" => "Y", "Param3" => $long_data);

    curl_setopt($c, CURLOPT_POSTFIELDS, $post);

    $response = curl_exec($c);

    echo $response;

    /*

    Response:

    HTTP/1.1 100 Continue

    HTTP/1.1 500 Internal Server Error
    Date: Tue, 05 Apr 2011 14:11:51 GMT
    Server: Microsoft-IIS/6.0
    X-Powered-By: ASP.NET
    X-AspNet-Version: 2.0.50727
    Cache-Control: private
    Content-Type: text/plain; charset=utf-8
    Content-Length: 100

    Request format is invalid: multipart/form-data; boundary=----------------------------5d738237d9e0.

    */
?>

For those who got here via google, like I did, I found an article that solved the problem.

http://www.developersalley.com/blog/post/2011/06/22/SystemInvalidOperationException-Request-format-is-invalid-multipartform-data-boundary-Error-When-Calling-Net-Web-Service-from-PHP.aspx

Basically, you have to stringify the data array

$post_array = array(
        "Param1"=>'xyz',
        "Param2"=>'abc',
        "Param3"=>'123'
);

//url-ify the data
foreach($post_array as $key=>$value) 
{ 
    $post_array_string .= $key.'='.$value.'&'; 
}
$post_array_string = rtrim($post_array_string,'&');
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_POST,count($post_array ));
curl_setopt($ch,CURLOPT_POSTFIELDS,$post_array_string);

Had a similar problem, with sending a XML to a IIS.

Solved using this php function: http_build_query($params);

It seems that IIS needs the post data to be in URL style.

This one is really similar to Tobias Fünke solution.

So you can use the function this way:

$post_array = array(
        "Param1"=>'xyz',
        "Param2"=>'abc',
        "Param3"=>'123'
);

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_POST,count($post_array));
curl_setopt($ch,CURLOPT_POSTFIELDS, http_build_query($post_array_string));

Hope this helps.

A little bit more tricky without knowing full details, the landing server for your cURL script might be denying access. EG:

curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; Linux x86_64; rv:2.2a1pre) Gecko/20110324 Firefox/4.2a1pre');

Just add that, see if it helps as if you're not sending a user_agent over, or an unrecognised 1 is picked up, maybe they are denying access.

Reading the response it looks like the server wants to see the form data sent over with a different Content-Type. It might need to be set to multipart/form-data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM