【问题标题】:Getting blank array from web service从 Web 服务获取空白数组
【发布时间】:2016-12-31 03:13:29
【问题描述】:

我在使用我在另一台服务器上创建的 Web 服务时遇到了一个非常奇怪的问题。

这是我创建的 Web 服务的 URL

http://52.53.227.143/API_test.php?post_name=BSN%20Syntha-6%20Isolate

我无法在我的网站上接收数组或任何其他格式的数据。我同时使用了 file_get_content 和 curl 来接收 json 格式,但它只给了我空白数组。

这是我的代码:

$post_name = 'BSN Syntha-6 Isolate';
$base = "http://52.53.227.143/API_test.php?post_name=".$post_name;
$str = file_get_contents($base);
echo '<pre>';
print_r(json_decode($str));

它给了我以下结果:

stdClass Object
(
[info] => Array
    (
    )

)

我也用Curl,代码如下:

$curl = curl_init();
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_URL, $base);
curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3'); 
curl_setopt($curl, CURLOPT_REFERER, 'http://www.google.com');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
$str = curl_exec($curl);
curl_close($curl);
echo '<pre>';
print_r(json_decode($str));

请帮帮我。

【问题讨论】:

  • 或许显示你的脚本的实际代码? “尽快”在这里也不是正确的心态。
  • 我没有看到任何代码。
  • 我重新发布了我的问题。

标签: php web-services curl


【解决方案1】:

使用 urlencode()

现在正在运行:

<?php
    $post_name = 'BSN Syntha-6 Isolate';
    $base = "http://52.53.227.143/API_test.php?post_name=" . urlencode($post_name);
    $str = file_get_contents($base);
    echo '<pre>';
    print_r(json_decode($str));
?>

【讨论】:

  • 谢谢,它现在可以同时使用 curl 和 file_get_content()。非常感谢
  • 没问题 :) 如果您有答案,您可以将问题标记为现在已解决!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-24
  • 2017-04-11
相关资源
最近更新 更多