PHP cURL错误:“来自服务器的空回复” [英] PHP cURL error: "Empty reply from server"

查看:331
本文介绍了PHP cURL错误:“来自服务器的空回复”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类函数,用于与 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,显然
),$ options);

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

//初始化cURL请求并设置参数
$ 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(兼容; MSIE 5.01; Windows NT 5.0)'


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

这会返回 code>。我知道有人建议这个错误来自网络基础设施的一些故障;我不相信这是真的在我的情况。如果我通过命令行运行cURL请求,我得到我的数据; Last.FM服务已启动并可访问。



在我去那些人之前,看看是否有什么改变,我想和你们一起检查一下,



ANSWER:

强的> @Jan Kuboschek帮助我绊倒什么是(可能)在这里。通过给予 CURLOPT_POSTFIELDS 关联数组,指定了特定的内容类型,它可能无法与某些RESTful服务一起使用。更聪明的解决方案是手动创建该数据的URL编码版本,并将其作为 CURLOPT_POSTFIELDS



有关详情,请查看: http://www.brandonchecketts.com / archives / array-versus-string-in-curlopt_postfields

解决方案

开始,中间或尾随。


$ b

$ b

我跑了你的代码,有同样的问题 - 没有输出。我试过URL和一个GET请求,服务器对我说。我将执行以下操作:


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


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


编辑
我已将您的代码移出课堂,因为我没有其余和修改它。以下代码对我来说是完美的。如果这些更改对您无效,我建议您的错误在不同的功能。

 <?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,显然
));

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

//初始化cURL请求并设置参数
$ 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();

?>


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:

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.

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_POSTFIELDS an 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.

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

解决方案

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

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:

  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.

Edit I 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();

?>

这篇关于PHP cURL错误:“来自服务器的空回复”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆