dongmisa4779 2010-08-27 11:50
浏览 76
已采纳

使用php curl消耗RESTful WCF Web服务的问题

I am having problems consuming a WCF web service (PaySimple's v3.00 API) using php curl. I am trying to post an xml payload to the service and I keep getting a 400 - Bad Request error. When I using Firefox's Poster add-on, I can post the very same payload and I get the expected response - 200 OK with an XML response containing the required response. Here's a snippet of my code:

        $ch = curl_init();
        $payload = $createPayload();
        $length = strlen($payload);
        $headers = array("Expect:");
        if($isPost)
        {
            curl_setopt($ch, CURLOPT_POST, TRUE);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
            $headers[] = "Content-length: $length";
        }
        else {
            curl_setopt($ch, CURLOPT_HTTPGET, TRUE);
        }
        if($requestType == 'xml')
        {
            $headers[] = 'Content-Type: text/xml; charset: utf-8';
            $headers[] = 'Accept: application/xml';
        }
        else {
            $headers[] = 'Content-Type: application/json; charset: utf-8';
            $headers[] = 'Accept: application/json';
        }            
        $headers[] = "Connection: close";
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
        //debugging
        curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
        $fh = fopen('curl_debug.txt', 'w');
        curl_setopt($ch, CURLOPT_STDERR, $fh);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);            
        $response = curl_exec($ch);
        curl_close($ch);

$payload contains the following value:

<AddCustomer xmlns="http://api.paysimple.com"><userkey>APIUser1989</userkey><apikey>pxTbp4F6y7JuaMu36qazEw51JiqUA7lGme0j62r3UeQyNojvEfpNo7tn4pLq0yEULHz5KykHWV6AjNQJEyskCw0tXSKZ0vcP5mCynh1IgfQEH4NdXRkXeMNAGEdx6Ee0</apikey><customer xmlns="http://api.paysimple.com" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><ApiConsumerData/><PsReferenceId>0</PsReferenceId><AltEmail>testemail@paysimpledev.com</AltEmail><AltPhone/><BillingAddress1>TestBillingAddress1</BillingAddress1><BillingAddress2/><BillingCity>TestBillingCity</BillingCity><BillingCountryCode/><BillingPostalCode>80202</BillingPostalCode><BillingState>6</BillingState><CompanyName>TestCompany</CompanyName><CreatedOn>0001-01-01T00:00:00</CreatedOn><Email>testemail@paysimpledev.com</Email><Fax/><FirstName>TestFirstName</FirstName><LastModified>0001-01-01T00:00:00</LastModified><LastName>TestLastName</LastName><MiddleName/><Notes/><Phone>5551234567</Phone><ShippingAddress1/><ShippingAddress2/><ShippingCity/><ShippingCountryCode/><ShippingPostalCode/><ShippingSameAsBilling>true</ShippingSameAsBilling><ShippingState>0</ShippingState><WebSite/></customer></AddCustomer>

Here's what shows up in curl_debug.txt:

  • About to connect() to sandbox-api.paysimple.com port 443 (#0)
  • Trying 165.193.50.51... * connected
  • Connected to sandbox-api.paysimple.com (165.193.50.51) port 443 (#0)
  • successfully set certificate verify locations:
  • CAfile: none CApath: /etc/ssl/certs
  • SSL connection using RC4-MD5
  • Server certificate:
  • subject: C=US; ST=Colorado; L=Denver; O=PaySimple, Inc.; OU=PaySimple; CN=*.paysimple.com
  • start date: 2010-05-04 00:00:00 GMT
  • expire date: 2011-06-29 23:59:59 GMT
  • issuer: C=US; O=DigiCert Inc; OU=www.digicert.com; CN=DigiCert High Assurance CA-3
  • SSL certificate verify ok.

POST /3.00/paysimpleapi.svc/xml/listcustomers HTTP/1.1
Host: sandbox-api.paysimple.com
Content-length: 1153
Content-Type: text/xml; charset: utf-8
Accept: application/xml
Connection: close

HTTP/1.1 400 Bad Request
Connection: close
Date: Fri, 27 Aug 2010 11:07:21 GMT
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
X-AspNet-Version: 2.0.50727
Set-Cookie: ASP.NET_SessionId=oxpese454u4rr3552evybjue; path=/; HttpOnly
Cache-Control: private
Content-Length: 0

Closing connection #0

F1! F1!

  • 写回答

3条回答 默认 最新

  • dora1989 2010-08-27 12:09
    关注

    The problem is probably in one of these:

    Content-Type: text/xml; charset: utf-8
    Accept: application/xml
    

    Try text/xml or application/xml for both, try uppercase UTF-8. Try to send the same headers as Firefox.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛