PHP 如何从网络服务获取内容?
How PHP get the content from web service?
我在从 Web 服务获取 content/array 到我的 php 代码时遇到问题。当我在浏览器中输入 url 如 http://server1-xeon.asuscomm.com/currency/?amount=1.20,from=MYR,to=SGD 时,浏览器中的结果显示如下: [ 1.20MYR , 0.39SGD ]。我的 PHP 代码如下所示:
$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;
不幸的是,我没有使用上面的代码。寻求帮助。
谢谢
已更新
$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;
此页面包含由 JavaScript 加载的动态内容。您可以在 Google Chrome 中看到它,例如通过访问 view-source:http://server1-xeon.asuscomm.com/currency/?amount=1.20,from=MYR,to=SGD.
如果您仔细查看页面的源代码(文件 http://server1-xeon.asuscomm.com/currency/JQuery/app_converter.js),您会发现它使用以下代码在幕后获取交换数据:
$.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 + ' ] ');
});
因此,实际上您可以在 PHP 中提出此类请求,以避免访问 http://server1-xeon.asuscomm.com/currency/?amount=1.20,from=MYR,to=SGD
页面上的动态内容。
更新:
我添加了更完整的解释。
到 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 上的页面通过 curl
时,在 JSON 正文中传递 amount
、fromCurrency
和 toCurrency
以及然后使用 json_decode($content);
.
解码收到的 JSON 响应
数据应该如何显示?从 "UPDATED" 和 运行:
将其添加到您的代码中
$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));
在网络浏览器中您将看到:
Array
(
[MYR] => 1.2
[SGD] => 0.39036
)
{"MYR":1.2,"SGD":0.39036}
暂时无法理解您的问题。
如果您只想打印返回值(数字),请执行此操作:echo $obj->d;
我在从 Web 服务获取 content/array 到我的 php 代码时遇到问题。当我在浏览器中输入 url 如 http://server1-xeon.asuscomm.com/currency/?amount=1.20,from=MYR,to=SGD 时,浏览器中的结果显示如下: [ 1.20MYR , 0.39SGD ]。我的 PHP 代码如下所示:
$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;
不幸的是,我没有使用上面的代码。寻求帮助。 谢谢
已更新
$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;
此页面包含由 JavaScript 加载的动态内容。您可以在 Google Chrome 中看到它,例如通过访问 view-source:http://server1-xeon.asuscomm.com/currency/?amount=1.20,from=MYR,to=SGD.
如果您仔细查看页面的源代码(文件 http://server1-xeon.asuscomm.com/currency/JQuery/app_converter.js),您会发现它使用以下代码在幕后获取交换数据:
$.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 + ' ] ');
});
因此,实际上您可以在 PHP 中提出此类请求,以避免访问 http://server1-xeon.asuscomm.com/currency/?amount=1.20,from=MYR,to=SGD
页面上的动态内容。
更新: 我添加了更完整的解释。
到 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 上的页面通过 curl
时,在 JSON 正文中传递 amount
、fromCurrency
和 toCurrency
以及然后使用 json_decode($content);
.
数据应该如何显示?从 "UPDATED" 和 运行:
将其添加到您的代码中$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));
在网络浏览器中您将看到:
Array
(
[MYR] => 1.2
[SGD] => 0.39036
)
{"MYR":1.2,"SGD":0.39036}
暂时无法理解您的问题。
如果您只想打印返回值(数字),请执行此操作:echo $obj->d;