curl - How PHP get the content from web service? -
i have problem in getting content/array web service php code. when type in url in browser http://server1-xeon.asuscomm.com/currency/?amount=1.20,from=myr,to=sgd, result in browser displayed this: [ 1.20myr , 0.39sgd ]. php code looks 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, nothing use code above. looking 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;
this page contains dynamic content, loaded javascript. can see in google chrome example access view-source:http://server1-xeon.asuscomm.com/currency/?amount=1.20,from=myr,to=sgd.
if closer source code of page (file http://server1-xeon.asuscomm.com/currency/jquery/app_converter.js) you'll see, uses code exchange data under hood:
$.ajax({type: "post", url: "webservice.asmx/yahoo_currencyex", // important: it's relative url! data: "{amount:" + amount + ",fromcurrency:'" + + "',tocurrency:'" + + "'}", contenttype: "application/json; charset=utf-8", datatype: "json", beforesend: function () {}, success: function (data) { $('#results').html(' [ ' + amount + + ' , ' + data.d.tofixed(2) + + ' ] '); });
so, can make such request in php avoid access dynamic content on http://server1-xeon.asuscomm.com/currency/?amount=1.20,from=myr,to=sgd
page.
upd: i've added more complete explanation.
by time page on http://server1-xeon.asuscomm.com/currency/?amount=1.20,from=myr,to=sgd loaded, javascript code on page (it means it's executed on client side, in browser, not on server) parses url parameters , makes ajax post
request url http://server1-xeon.asuscomm.com/currency/webservice.asmx/yahoo_currencyex. passes json payload {amount:1.20,fromcurrency:'myr',tocurrency:'sgd'}
, gets response {"d":0.390360}
. so, can make direct post
request http://server1-xeon.asuscomm.com/currency/webservice.asmx/yahoo_currencyex via curl
, passing amount
, fromcurrency
, tocurrency
in json body , decode received json response using json_decode($content);
.
Comments
Post a Comment