PHP cURL 错误:“来自服务器的空回复”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3018378/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 08:26:07  来源:igfitidea点击:

PHP cURL error: "Empty reply from server"

phpcurllast.fm

提问by ABach

I have a class function to interface with the RESTful API for Last.FM- its purpose is to grab the most recent tracks for my user. Here it is:

我有一个类函数可以与Last.FM的 RESTful API 接口- 它的目的是为我的用户获取最新的曲目。这里是:

private static $base_url = 'http://ws.audioscrobbler.com/2.0/';

public static function getTopTracks($options = array())
{
  $options = array_merge(array(
    'user' => 'bachya',
    'period' => NULL,
    'api_key' => 'xxxxx...', // obfuscated, obviously
  ), $options);

  $options['method'] = 'user.getTopTracks';

  // Initialize cURL request and set parameters
  $ch = curl_init();
  curl_setopt_array($ch, array(
    CURLOPT_URL            => self::$base_url,
    CURLOPT_POST           => TRUE,
    CURLOPT_POSTFIELDS     => $options,
    CURLOPT_RETURNTRANSFER => TRUE,
    CURLOPT_TIMEOUT        => 30,
    CURLOPT_USERAGENT      => 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)'
  ));

  $results = curl_exec($ch);
  return $results;
}

This returns "Empty reply from server". I know that some have suggested that this error comes from some fault in network infrastructure; I do not believe this to be true in my case. If I run a cURL request through the command line, I get my data; the Last.FM service is up and accessible.

这返回"Empty reply from server". 我知道有些人认为这个错误来自网络基础设施的某些故障;我不相信这在我的情况下是正确的。如果我通过命令行运行 cURL 请求,我会得到我的数据;Last.FM 服务已启动并可访问。

Before I go to those folks and see if anything has changed, I wanted to check with you fine folks and see if there's some issue in my code that would be causing this.

在我去找那些人并查看是否有任何变化之前,我想与你们这些好人核对一下,看看我的代码中是否存在导致这种情况的问题。

Thanks!

谢谢!

ANSWER:@Jan Kuboschek helped me stumble onto what is (maybe) going on here. By giving CURLOPT_POSTFIELDSan associative array, a particular content-type is specified that may not work with certain RESTful services. A smarter solution is to manually create a URL-encoded version of that data and pass that as the CURLOPT_POSTFIELDS.

回答:@Jan Kuboschek 帮助我偶然发现了(可能)这里发生的事情。通过提供CURLOPT_POSTFIELDS关联数组,指定了可能不适用于某些 RESTful 服务的特定内容类型。一个更聪明的解决方案是手动创建该数据的 URL 编码版本并将其作为CURLOPT_POSTFIELDS.

For more info, check out: http://www.brandonchecketts.com/archives/array-versus-string-in-curlopt_postfields

有关更多信息,请查看:http: //www.brandonchecketts.com/archives/array-versus-string-in-curlopt_postfields

回答by Jan Kuboschek

A common issue are spaces in the URL - beginning, in the middle, or trailing. Did you check that out?

一个常见问题是 URL 中的空格 - 开头、中间或结尾。你检查了吗?

Edit- per comments below, spacing is not the issue.

编辑- 根据下面的评论,间距不是问题。

I ran your code and had the same problem - no output whatsoever. I tried the URL and with a GET request, the server talks to me. I would do the following:

我运行了您的代码并遇到了同样的问题 - 没有任何输出。我尝试了 URL 并通过 GET 请求,服务器与我交谈。我会做以下事情:

  1. Use the following as $base_url: $base_url = 'http://ws.audioscrobbler.com/2.0/?user=bachya&period=&api_key=xxx&method=user.getTopTracks';

  2. Remove the post fields from your request.

  1. 使用以下作为 $base_url: $base_url = ' http://ws.audioscrobbler.com/2.0/?user=bachya&period=&api_key=xxx&method=user.getTopTracks';

  2. 从您的请求中删除帖子字段。

EditI moved your code out of the class since I didn't have the rest and modified it. The following code runs perfect for me. If these changes don't work for you, I suggest that your error is in a different function.

编辑我把你的代码移出了课堂,因为我没有剩下的代码并修改了它。以下代码对我来说非常完美。如果这些更改对您不起作用,我建议您的错误出现在不同的函数中。

<?php


function getTopTracks()
{
  $base_url = 'http://ws.audioscrobbler.com/2.0/?user=bachya&period=&api_key=8066d2ebfbf1e1a8d1c32c84cf65c91c&method=user.getTopTracks';
  $options = array_merge(array(
    'user' => 'bachya',
    'period' => NULL,
    'api_key' => 'xxxxx...', // obfuscated, obviously
  ));

  $options['method'] = 'user.getTopTracks';

  // Initialize cURL request and set parameters
  $ch = curl_init($base_url);
  curl_setopt_array($ch, array(
    CURLOPT_URL            => $base_url,
    CURLOPT_RETURNTRANSFER => TRUE,
    CURLOPT_TIMEOUT        => 30,
    CURLOPT_USERAGENT      => 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)'
  ));

  $results = curl_exec($ch);
  return $results;
}

echo getTopTracks();

?>

回答by cygri

The server received your request, but sent an empty response. Check the result of curl_getinfo($ch, CURLINFO_HTTP_CODE)to find out if the server responded with an HTTP error code.

服务器收到您的请求,但发送了一个空响应。检查结果curl_getinfo($ch, CURLINFO_HTTP_CODE)以了解服务器是否以 HTTP 错误代码响应。

Update:Ok so the server responds with the 100 ContinueHTTP status code. In that case, this should solve your problem:

更新:好的,服务器以100 ContinueHTTP 状态代码响应。在这种情况下,这应该可以解决您的问题:

curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));

I found this here: PHP and cURL: Disabling 100-continue header. Hope it works!

我在这里找到了这个:PHP and cURL: Disabling 100-continue header。希望它有效!

回答by nmeegama

I came acorss the same issue. My Http_code returned 200 but my response was empty. There could be many reasons for this as i experienced.

我遇到了同样的问题。我的 Http_code 返回 200 但我的响应为空。根据我的经验,这可能有很多原因。

--Your hedaers might be incorrect

--你的hedaers可能不正确

CURLOPT_HTTPHEADER => array('Content-Type:application/json', 'Expect:')

--You might need to send data as post fields in culr and not attached to the URl like url?p1=a1&p2=a2

--您可能需要将数据作为 culr 中的 post 字段发送,而不是像url?p1=a1&p2=a2那样附加到 URl

$data = array (p1=>a1, p2=>a2)
CURLOPT_POSTFIELDS => $data

So your options array would be similar to the below

所以你的选项数组将类似于下面

array(
    CURLOPT_URL => $url,
    CURLOPT_FAILONERROR => TRUE, // FALSE if in debug mode
    CURLOPT_RETURNTRANSFER => TRUE,
    CURLOPT_TIMEOUT => 4,
    CURLOPT_HTTPHEADER => array('Content-Type:application/json', 'Expect:'),
    CURLOPT_POST => TRUE,
    CURLOPT_POSTFIELDS => $data,
);

回答by Micha? Pipa

According to Last.FM API documentationyou should use GET method instead of POST to pass parameters. When I've changed POST to GET I've received the answer about incorrect key.

根据 Last.FM API 文档,您应该使用 GET 方法而不是 POST 来传递参数。当我将 POST 更改为 GET 时,我收到了有关不正确密钥的答案。

回答by Fabiano Shark

And here's the code for get Album Infofrom Laft.FM even if return error:

这是从 Laft.FM获取专辑信息的代码,即使返回错误:

The Function:

功能:

function getAlbum($xml,$artist,$album)
{
  $base_url = $xml;
  $options = array_merge(array(
    'user' => 'YOUR_USERNAME',
    'artist'=>$artist,
    'album'=>$album,
    'period' => NULL,
    'api_key' => 'xYxOxUxRxxAxPxIxxKxExYxx', 
  ));

  $options['method'] = 'album.getinfo';

  // Initialize cURL request and set parameters
  $ch = curl_init($base_url);
  curl_setopt_array($ch, array(
    CURLOPT_URL            => 'http://ws.audioscrobbler.com/2.0/',
    CURLOPT_POST           => TRUE,
    CURLOPT_POSTFIELDS     => $options,
    CURLOPT_RETURNTRANSFER => TRUE,
    CURLOPT_TIMEOUT        => 30,
    CURLOPT_HTTPHEADER        => array( 'Expect:' ) ,
    CURLOPT_USERAGENT      => 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)'
  ));

  $results = curl_exec($ch);
  unset ($options);
  return $results;
}

Usage:

用法:

// Get the XML
$xml_error = getAlbum($xml,$artist,$album);

// Show XML error
if (preg_match("/error/i", $xml_error)) {
    echo " <strong>ERRO:</strong> ".trim(strip_tags($xml_error));
}