简体   繁体   English

对IIS的PHP Curl请求以请求格式无效

[英]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. 我正在尝试使用curl访问第三者网络服务,我使用了以下代码,如果我在自己的linux服务器上尝试使用它,则数据运行良好,但是第三者服务器上的IIS返回错误。

$longdata is a long string of data, maybe over 1000 characters long $ longdata是一长串数据,可能超过1000个字符

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. 对于那些像我一样通过google到达这里的人,我找到了解决该问题的文章。

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 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. 在将XML发送到IIS时遇到了类似的问题。

Solved using this php function: http_build_query($params); 使用以下php函数解决:http_build_query($ params);

It seems that IIS needs the post data to be in URL style. 似乎IIS需要发布数据采用URL样式。

This one is really similar to Tobias Fünke solution. 这确实类似于TobiasFünke解决方案。

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. 在不了解全部详细信息的情况下,有些棘手的问题,您的cURL脚本的着陆服务器可能拒绝访问。 EG: 例如:

curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; Linux x86_64; rv:2.2a1pre) Gecko/20110324 Firefox/4.2a1pre'); 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. 只需添加一下,看看是否有帮助,就好像您没有发送user_agent一样,或者拾取了无法识别的1,也许他们正在拒绝访问。

Reading the response it looks like the server wants to see the form data sent over with a different Content-Type. 读取响应后,服务器似乎希望查看使用其他Content-Type发送的表单数据。 It might need to be set to multipart/form-data. 可能需要将其设置为multipart / form-data。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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