简体   繁体   English

Json POST卷曲PHP

[英]Json POST To Curl PHP

I'm trying to fetch some data from this json javascript code and print them to PHP using CURL 我正在尝试从此json javascript代码中获取一些数据,并使用CURL将它们打印到PHP

<html>
<script src="//code.jquery.com/jquery-1.12.0.min.js"></script>
<script src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
<body>
<script type="text/javascript">
jQuery.ajax(
   {
       type:"POST",
       contentType:"application/json;charset=utf-8",
       url:"https://www.bancopromerica.com.gt/wsservicebus/wsonlineservicebus.asmx/getTipoCambio",
       data:"{}",
       dataType:"json",
       async: false,
       success: function(msg) {
          a("#compInter",msg.d.compraInternet); //Compra Internacional
          a("#ventInter",msg.d.ventaInternet); //Venta Internacional
          a("#compAgencia",msg.d.compraAgencia); //Compra Agencia
          a("#ventAgencia",msg.d.ventaAgencia); //Venta Agencia
      },
      error: function(textStatus, errorThrown, errorDetail){
          alert(errorDetail);
      }
   });
function a(a,b)
{
   jQuery(a).append(b);
}
</script>
</body>
</html>

I receive this error: 我收到此错误:

[Exception... "Failure" nsresult: "0x80004005 (NS_ERROR_FAILURE)" location: "JS frame :: http://code.jquery.com/jquery-1.12.0.min.js :: .send :: line 4" data: no] [异常...“失败” nsresult:“ 0x80004005(NS_ERROR_FAILURE)”位置:“ JS框架:: http://code.jquery.com/jquery-1.12.0.min.js :: .send ::第4行资料:否]

Do you have any idea how to do this right in PHP ? 您知道如何在PHP中正确执行此操作吗?

I think all of your JQuery.ajax is correct(By the way, you can change it to $.ajax ). 我认为您所有的JQuery.ajax都是正确的(顺便说一句,您可以将其更改为$.ajax )。 The problem that I am thinking is in your a function. 我正在考虑的问题在于您a功能。 This is how I think it should look, and how I do it: 这是我认为的样子,以及我的做法:

function a(a, b)
{
    $(a).html(b);
}

I have not checked it out on codepin, or jsfiddle, or anything, but I think that will work. 我还没有在Codepin或jsfiddle或其他任何工具上进行过检查,但是我认为这可以工作。

A very basic php cURL example would be: 一个非常基本的php cURL示例将是:

    // create curl resource 
    $ch = curl_init(); 

    // set url 
    curl_setopt($ch, CURLOPT_URL, "https://www.bancopromerica.com.gt/wsservicebus/wsonlineservicebus.asmx/getTipoCambio"); 

    //return the transfer as a string 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 

    // $output contains the output string 
    $output = curl_exec($ch); 

    // close curl resource to free up system resources 
    curl_close($ch); 

    echo json_encode($output)

You would need to var_dump the output as you will want to assign array key/values that suit your needs. 您将需要var_dump output因为您想分配适合您需要的数组键/值。

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

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