-1

I'm trying to fetch some data from this json javascript code and print them to PHP using CURL

<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]

Do you have any idea how to do this right in PHP ?

bjesua
  • 319
  • 1
  • 4
  • 14

2 Answers2

2

I think all of your JQuery.ajax is correct(By the way, you can change it to $.ajax). The problem that I am thinking is in youra function. 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.

Fishy
  • 1,275
  • 1
  • 12
  • 26
  • Thank you, i make the changes but still not working! – bjesua Apr 19 '16 at 19:43
  • By the way, whenever I go to your website I get this error: `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. ` – Fishy Apr 19 '16 at 19:53
  • thank you so much, well that's why i want to consume with Curl PHP becanse aparently this can't be work witht the same origin code, it just can be working with different code, that's my problem. That's why i want to find a solution to make it work with PHP – bjesua Apr 19 '16 at 20:00
1

A very basic php cURL example would be:

    // 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.

Kevin Lynch
  • 24,427
  • 3
  • 36
  • 37
  • Thank you so much, that's what i'm looking for, i'm understanding a little bit more now, i've tried this example right now, it's printing this error : "\r\n \r\n what you think about it? – bjesua Apr 19 '16 at 20:02