使用cURL和PHP的Soap请求

时间:2013-05-15 14:28:18

标签: php api soap curl wsdl

我正在尝试通过PHP中的Curl发送SOAP帖子,但我总是得到一个无法连接到主机问题。 但是当我想在基于网络的客户端中使用相同的URL(如投掷)时,得到了正确的答案:

  • Hurl测试:http://www.hurl.it
  • 我的代码:

        $url = "https://gateway.monster.com/bgwBroker";
    
        $soapMessage = '<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">' .
            '<SOAP-ENV:Header>'.
            '<mh:MonsterHeader xmlns:mh="http://schemas.monster.com/MonsterHeader">'.
              '<mh:MessageData>'.
                '<mh:MessageId>PresenceMedia SARL Jobs</mh:MessageId>'.
                '<mh:Timestamp>2004-06-09T14:41:44Z</mh:Timestamp>'.
              '</mh:MessageData>'.
            '</mh:MonsterHeader>'.
            '<wsse:Security xmlns:wsse="http://schemas.xmlsoap.org/ws/2002/04/secext">'.
              '<wsse:UsernameToken>'.
                '<wsse:Username>xrtpjobsx01</wsse:Username>'.
                '<wsse:Password>rtp987654</wsse:Password>'.
              '</wsse:UsernameToken>'.
            '</wsse:Security>'.
          '</SOAP-ENV:Header>'.
          '<SOAP-ENV:Body>'.
            '<Job jobRefCode="Job - minimal fields" jobAction="addOrUpdate" jobComplete="true" xmlns="http://schemas.monster.com/Monster" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://schemas.monster.com/Monster http://schemas.monster.com/Current/xsd/Monster.xsd">'.
              '<RecruiterReference>'.
                '<UserName>xrtpjobsx01</UserName>'.
              '</RecruiterReference>'.
              '<JobInformation>'.
                '<JobTitle><![CDATA[PresenceMedia SARL its a simple test from morocco blablablablablablablablablablablabla]]></JobTitle>'.
                '<JobStatus monsterId="4">JobTypeFullTime</JobStatus>'.
                '<PhysicalAddress>'.
                  '<City>Rabat Shore</City>'.
                  '<State>NY</State>'.
                  '<CountryCode>US</CountryCode>'.
                  '<PostalCode>11220</PostalCode>'.
                '</PhysicalAddress>'.
                '<JobBody><![CDATA[PresenceMedia SARL Body blablabla blablabla blablabla blablabla blablabla blablabla blablabla]]></JobBody>'.
              '</JobInformation>'.
              '<JobPostings>'.
                '<JobPosting>'.
                  '<Location>'.
                    '<City>London East</City>'.
                    '<State>London</State>'.
                    '<CountryCode>UK</CountryCode>'.
                  '</Location>'.
                  '<JobCategory monsterId="47" />'.
                  '<JobOccupations>'.
                    '<JobOccupation monsterId="11909" />'.
                  '</JobOccupations>'.
                  '<BoardName monsterId="1" />'.
                  '<Industries>'.
                    '<Industry>'.
                      '<IndustryName monsterId="1" />'.
                    '</Industry>'.
                  '</Industries>'.
                '</JobPosting>'.
              '</JobPostings>'.
            '</Job>'.
          '</SOAP-ENV:Body>'.
        '</SOAP-ENV:Envelope>';
    
        $headers = array(             
                "Content-type: text/xml;charset=\"utf-8\"", 
                "Accept: text/xml", 
                "Cache-Control: no-cache", 
                "Pragma: no-cache", 
                "SOAPAction: \"run\"", 
                "Content-length: ".strlen($soapMessage),
            ); 
    
        $soapUser = "xrtpjobsx01";
        $soapPassword = "rtp987654";
    
        $soap_do = curl_init();
        curl_setopt($soap_do, CURLOPT_URL, $url);
        curl_setopt($soap_do, CURLOPT_PORT, 8443);
        curl_setopt($soap_do, CURLOPT_USERPWD, $soapUser.":".$soapPassword);
        curl_setopt($soap_do, CURLOPT_RETURNTRANSFER, true );
        curl_setopt($soap_do, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($soap_do, CURLOPT_SSL_VERIFYHOST, false);   
        curl_setopt($soap_do, CURLOPT_POST,           true );        
        curl_setopt($soap_do,CURLOPT_TIMEOUT,9000);    
        curl_setopt($soap_do, CURLOPT_POSTFIELDS,     $soapMessage); 
        curl_setopt($soap_do, CURLOPT_HTTPHEADER,     $headers);
    
    
        $result = curl_exec($soap_do);
        $err = curl_error($soap_do);
    
        echo "Result:" . $result;
        echo "<br>Error:" . $err ;
    
        echo "<b><pre>";
        var_dump(curl_getinfo($soap_do));
        echo "</pre>";
    
    
        ?>
    

1 个答案:

答案 0 :(得分:1)

在php中使用简单的Soapclient发布响应。并检查响应还检查用于连接的端口是否空闲。

$client = new SoapClient($Url, $options);
$data = $client->functionName($params);