【问题标题】:Json POST To Curl PHPJson POST 卷曲 PHP
【发布时间】:2016-08-12 04:19:56
【问题描述】:

我正在尝试从这个 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>

我收到此错误:

[异常...“失败”nsresult:“0x80004005 (NS_ERROR_FAILURE)” 位置:“JS框架::http://code.jquery.com/jquery-1.12.0.min.js:: .send :: line 4" 数据:无]

您知道如何在 PHP 中正确执行此操作吗?

【问题讨论】:

  • 您有一个div 和一个id 的compInter。这将附加值msg.d.compraInternet。这不工作吗?
  • PHP 与此有什么关系?您有一些 JavaScript 和一个指向 ASP.NET Web 服务的 URL。
  • @steve 不,我有一个错误..
  • @Quentin 我需要使用 CURL,ASP.net 只是一个例子,它可以很好地与 web 服务一起工作,但我需要让它与 PHP 一起工作
  • @rojocuarentaodiso — 你是在问如何用 PHP/cURL 代替 JavaScript 来做你目前正在做的事情吗?

标签: javascript php json curl


【解决方案1】:

我认为您所有的JQuery.ajax 都是正确的(顺便说一句,您可以将其更改为$.ajax)。我在想的问题出在你的a 函数中。这就是我认为它应该看起来的样子,以及我是如何做到的:

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

我还没有在 codepin、jsfiddle 或任何东西上检查过它,但我认为这会起作用。

【讨论】:

  • 谢谢,我做了更改,但仍然无法正常工作!
  • 顺便说一句,每当我访问您的网站时,我都会收到此错误:An application error occurred on the server. The current custom error settings for this application prevent the details of the application error from being viewed remotely (for security reasons). It could, however, be viewed by browsers running on the local server machine.
  • 非常感谢,这就是我想使用 Curl PHP 的原因,因为显然这不能使用相同的原始代码,它只能使用不同的代码,这是我的问题。这就是为什么我想找到一个解决方案来使它与 PHP 一起工作
【解决方案2】:

一个非常基本的 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)

您需要var_dump 和output,因为您需要分配适合您需要的数组键/值。

【讨论】:

  • 非常感谢,这就是我要找的东西,我现在明白了一点,我现在已经尝试了这个例子,它正在打印这个错误:“\r\n \r\n 你怎么看?
猜你喜欢
  • 2011-05-15
  • 2012-08-13
  • 2015-03-25
  • 2017-04-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-06
  • 2018-09-20
相关资源
最近更新 更多