doqdcks3948 2014-06-18 15:10
浏览 47
已采纳

Javascript将函数发布到PHP函数中。 发布到网络服务

I have been asked to turn the below Javascript function in to a PHP function.

I have never been asked to do anything like this previously and so have been struggling a lot and may have all the wrong terminology in relation to SOAP/WebServices etc so my apologies for any confusion.

var varValidationCode = "VALIDATIONCODE";
            var Email = document.getElementById('Email').value;
            //alert(Email);
            $.ajax(
            {
                url: 'http://www.DOMAIN.com/FILE.asmx',
                type: "POST",
                data: "{Email:'" + Email + "',ValidationCode:'" + varValidationCode + "'}",
                dataType: "json",
                contentType: "application/json; charset=utf-8",
                cache: false,
                success: function(data) {
                    alert(data.d);
                },
                error: function() {
                    alert("FAIL");
                }
            });
            //alert("Function Called Successfully");
        }

I tried to create a curl function to post the values like below, which I got from another post on stackoverflow but I get the error - "soap:ReceiverServer was unable to process request. ---> Data at the root level is invalid. Line 1, position 1."

 // Here is the data we will be sending to the service
  $data = array(
    'Email'     => 'TEST@TEST.COM',
    'ValidationCode' => 'SOMEVALIDATIONCODE'
  );  


  $curl = curl_init('http://www.DOMAINNAME.com/FILE.asmx');


  curl_setopt($curl, CURLOPT_POST, 1); //Choosing the POST method
  curl_setopt($curl, CURLOPT_URL, 'http://www.DOMAINNAME.com/FILE.asmx');  // Set the url path we want to call
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);  // Make it so the data coming back is put into a string
  curl_setopt($curl, CURLOPT_POSTFIELDS, $data);  // Insert the data

  // Send the request
  $result = curl_exec($curl);

  // Free up the resources $curl is using
  curl_close($curl);

  echo $result;

The file I am posting to looks like this:

<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="htt{://www.w3.org/2001/XMLSchema-instance" xmlns:xds="http://www.w3.org/2001/XMLSchema" xmlns:xds="http://www.w3.org/2001/05/soap-envelope">
    <soap:Body>
        <PaymentValidationInfo xmlns="http://tempuri.org/">
            <Email>STRING</Email>
            <ValidationCode>STRING</ValidationCode>
        </PaymentValidationInfo>
    <soap12:Body>
</soap12:Envelope>

I hope this all makes sense. Any help would be much appreciated. Regards

  • 写回答

1条回答 默认 最新

  • drep94225 2014-06-19 11:10
    关注

    I'm not sure if people are interested but I managed to work how to get what I wanted working by the below.

    $url = "http://www.domain.com/WebServices/CCPWebService.asmx/METHOD?";
    
    $data = array(
      'Email' => 'EMAIL',
      '&ValidationCode' => '4946565',
    );
    
    foreach($data as $key=>$value) { $content .= $key.'='.$value; }
    
    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_HEADER, false);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $content);
    
    $json_response = curl_exec($curl);
    
    $status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
    
    
    
    
    /* FOR DEBUG PURPOSES
    if(!curl_errno($curl))
    {
     $info = curl_getinfo($curl);
    
     echo 'Took ' . $info['total_time'] . ' seconds to send a request to ' . $info['url'].'<br/><br/>';
     echo "CURL ERROR NO : ".curl_errno($curl); //returns 7
    echo "<br />CURL STATUS : ".curl_getinfo($curl, CURLINFO_HTTP_CODE); //returns 0
    echo "<br />CURL ERROR : ".curl_error($curl); //returns "couldn't connect to host"
     echo "<br />STRINGS POSTED: ";print_r($content);
     echo "<br />CURL INFO : ";print_r($info);
    }*/
    
    
    
    
    
    curl_close($curl);
    

    If anyone can recommend any additions/best practices it would be much appreciated. Regards

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作