3

I have a problem in getting the content/array from web service to my php code. When I type in the url in browser like http://server1-xeon.asuscomm.com/currency/?amount=1.20,from=MYR,to=SGD, then the result in the browser is displayed like this: [ 1.20MYR , 0.39SGD ]. My PHP code looks like this:

$ch = curl_init('http://server1-xeon.asuscomm.com/currency/?amount=1.20,from=MYR,to=SGD');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$content = curl_exec($ch);
curl_close($ch);
echo $content;

Unfortunately, I get nothing use the code above. Looking for help. Thanks.

UPDATED

 $data=array(
'amount'=>1.2,
'fromCurrency'=>'MYR',
'toCurrency'=>'SGD'
 );

 $data_string = json_encode($data);

 $ch = curl_init('http://server1-xeon.asuscomm.com/currency/WebService.asmx/YaHOO_CurrencyEx');

 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');                                                                     
 curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                                                                  
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
 curl_setopt($ch, CURLOPT_HTTPHEADER, 
    array('Content-type: application/json'));                                                                                                       
$content= curl_exec($ch);
curl_close($ch);
$obj = json_decode($content);
echo $obj;
marian0
  • 664
  • 6
  • 15
JCChan
  • 465
  • 2
  • 6
  • 17
  • 2
    The result `[ 1.20MYR , 0.39SGD ]` isn't a proper JSON string (the values would need to be quoted) and the URL outputts the result in a HTML-table. More than that, it looks like the result gets generated by javascript on the server, which means that you will never get the result through cURL since the JS won't run. – M. Eriksson Jan 16 '16 at 09:58
  • mean that curl only work on get the JSON? – JCChan Jan 16 '16 at 11:00

2 Answers2

2

This page contains dynamic content, loaded by JavaScript. You can see it in Google Chrome for example by access view-source:http://server1-xeon.asuscomm.com/currency/?amount=1.20,from=MYR,to=SGD.

If you look closer to the source code of the page (file http://server1-xeon.asuscomm.com/currency/JQuery/app_converter.js) you'll see, that it uses this code to get exchange data under the hood:

$.ajax({type: "POST",
        url: "WebService.asmx/YaHOO_CurrencyEx",  // important: it's a relative URL!
        data: "{amount:" + amount + ",fromCurrency:'" + from + "',toCurrency:'" + to + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        beforeSend: function () {},
        success: function (data) {
            $('#results').html(' [ ' + amount + from + ' , ' + data.d.toFixed(2) + to + ' ] ');
        });

So, actually you can make such request in PHP to avoid access dynamic content on the http://server1-xeon.asuscomm.com/currency/?amount=1.20,from=MYR,to=SGD page.

UPD: I've added a more complete explanation.

enter image description here

By the time the page on http://server1-xeon.asuscomm.com/currency/?amount=1.20,from=MYR,to=SGD is loaded, a JavaScript code on this page (it means that it's executed on the client side, in a browser, not on your server) parses URL parameters and makes AJAX POST request to the URL http://server1-xeon.asuscomm.com/currency/WebService.asmx/YaHOO_CurrencyEx. It passes a JSON payload {amount:1.20,fromCurrency:'MYR',toCurrency:'SGD'} and gets a response like this {"d":0.390360}. So, you can just make a direct POST request to the http://server1-xeon.asuscomm.com/currency/WebService.asmx/YaHOO_CurrencyEx via curl, passing an amount, fromCurrency and toCurrency in JSON body and then decode received JSON response using json_decode($content);.

Ivan Velichko
  • 6,348
  • 6
  • 44
  • 90
  • Sorry,since i still new in php, so i no really understand....mean that I have to add two more line such as `$data=array('amount'=>1.2,'fromCurrency'=>'MYR','toCurrency'=>'SGD');` and `curl_setopt($ch, CURLOPT_HTTPHEADER,array('Content-Type:application/json','Content-Length: ' . strlen($data)));` to my php code? – JCChan Jan 16 '16 at 11:16
  • @JCChan, please, look at my update. Also, you don't have to specify `Content-Length` header manually. – Ivan Velichko Jan 16 '16 at 11:28
  • ,please have a look at my update.I already changed as your instruction and still get error page.Don't know what is the problem. – JCChan Jan 16 '16 at 13:41
  • @JCChan, your code looks absolutely OK. Just replace `echo $obj` to `var_dump($obj)` because PHP can't convert `$obj` to string. – Ivan Velichko Jan 16 '16 at 14:41
  • I still get error page,when i check using the Chrome's Inspect Element and get this **Provisional headers are shown** in my header and thereby the status is **failed** after a long-time loading. – JCChan Jan 16 '16 at 15:47
  • I'd appreciated for your thoroughly explanation indeed.Its work now. – JCChan Jan 16 '16 at 17:37
1

How data should look? Add this to your code from "UPDATED" and run:

$array["MYR"] = 1.2;
$array["SGD"] = doubleval($obj->d)

// Print simple array (print source for example)
echo "<pre>";
print_r($array);
echo "</pre>";

// Print true JSON-array
print_r(json_encode($array));

In web browser you will see:

Array
(
    [MYR] => 1.2
    [SGD] => 0.39036
)

{"MYR":1.2,"SGD":0.39036}

Can't understand your problem at this moment.

If you want print only returned value (digits), do it: echo $obj->d;

Vika Marquez
  • 353
  • 1
  • 3
  • 12
  • Thanks for your solution as well. This `echo $obj->d` help to get the result or value directly. – JCChan Jan 17 '16 at 14:49