Zend_Http_Client 和 cURL 正在删除换行符

发布于 2024-11-09 11:33:08 字数 1055 浏览 4 评论 0原文

我正在尝试从远程服务器获取 CSV 文件并使用 Zend_Http_Client 下载

它 获取的版本已删除所有换行符。

require_once('Zend/Http/Client.php');
$client = new Zend_Http_Client($url);
//also tried the curl adapter but no change

$client->setCookieJar();
$client->setAuth('user', 'pass', Zend_Http_Client :: AUTH_BASIC);

if(!empty($params)){
   $client->setParameterGet($params);
}


$client->request();
$request = $client->getLastRequest();
                                                                                                                                            

$response = $client->getLastResponse();
echo $response->getRawBody();

响应全是一行。

如果我使用curl 获取$url,它会位于不同的行上。

另外,我正在查看源代码,而不是 HTML 渲染版本

UPDATE

所以我使用 cURL 重写了该位,但它仍然做同样的事情!?

 if(!empty($params)){
      $queryString = http_build_query($params);
      $url.='?'.$queryString;
 }

 $ch = curl_init($url);

 curl_setopt($ch,CURLOPT_USERPWD,"$username:$password");
 curl_exec($ch);

任何想法

I'm trying to fetch a CSV file from a remote server and download it Using Zend_Http_Client

The fetched version has all of the newlines removed.

require_once('Zend/Http/Client.php');
$client = new Zend_Http_Client($url);
//also tried the curl adapter but no change

$client->setCookieJar();
$client->setAuth('user', 'pass', Zend_Http_Client :: AUTH_BASIC);

if(!empty($params)){
   $client->setParameterGet($params);
}


$client->request();
$request = $client->getLastRequest();
                                                                                                                                            

$response = $client->getLastResponse();
echo $response->getRawBody();

The response is all one line.

If I fetch the $url with curl it is on separate lines.

Also, I am looking at the source, not the HTML rendered version

UPDATE

So I rewrote that bit using cURL and it still does the same thing !?

 if(!empty($params)){
      $queryString = http_build_query($params);
      $url.='?'.$queryString;
 }

 $ch = curl_init($url);

 curl_setopt($ch,CURLOPT_USERPWD,"$username:$password");
 curl_exec($ch);

Any ideas

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我的影子我的梦 2024-11-16 11:33:09

为什么使用 getRawBody() 而不是 getBody()? rawBody() 通常不是您想要的,并且可能以某种形式进行编码。

无论如何,您可以发布从服务器获得的响应标头吗?另外,指向实际文件或其中几行的链接也会有所帮助。

$response = $client->getLastResponse();
echo $response->getHeadersAsString();

Why are you using getRawBody() and not getBody()? rawBody() is usually not the one you want, and might be encoded in some form.

In any case can you post the response headers you get from the server? Also a link to the actual file or a few lines of it would help.

$response = $client->getLastResponse();
echo $response->getHeadersAsString();
一生独一 2024-11-16 11:33:09

这并不是真正的答案,但解决方法是使用curl 系统调用。

看起来这是行结尾的问题,即使我设置了 ini 值,它们也没有被检测到。

 $urlArray = parse_url($url);

//put the params together
 if(!empty($params)){
      //split up any existing params
      $qsArray = parse_str($urlArray['query']);
      if(empty($qsArray)){
           $urlArray['query']=http_build_query($params);
      }
      else{
           $urlArray['query'] = http_build_query(array_merge($qsArray,$params));
      }
 }

 //set the username and password
 $urlArray['user']=$username;
 $urlArray['pass']=$password;


// http_build_url doesn't work so doing it by hand

 $urlString  = $urlArray['scheme'];
 $urlString .= "://";
 $urlString .= $urlArray['user'].':'.$urlArray['pass'] .'@';
 $urlString .= $urlArray['host'];
 $urlString .= $urlArray['path'];
 $urlString .= '?'.$urlArray['query'];

//     $urlString = http_build_url($urlArray);
//     echo($urlString);

//php is messing up the line endings, so using a system call
 return  `curl '$urlString'`;

Not really an answer, but a work around is to use the curl system call.

It looks like it is an issue with the line endings, they aren't getting detected even when I set the ini value.

 $urlArray = parse_url($url);

//put the params together
 if(!empty($params)){
      //split up any existing params
      $qsArray = parse_str($urlArray['query']);
      if(empty($qsArray)){
           $urlArray['query']=http_build_query($params);
      }
      else{
           $urlArray['query'] = http_build_query(array_merge($qsArray,$params));
      }
 }

 //set the username and password
 $urlArray['user']=$username;
 $urlArray['pass']=$password;


// http_build_url doesn't work so doing it by hand

 $urlString  = $urlArray['scheme'];
 $urlString .= "://";
 $urlString .= $urlArray['user'].':'.$urlArray['pass'] .'@';
 $urlString .= $urlArray['host'];
 $urlString .= $urlArray['path'];
 $urlString .= '?'.$urlArray['query'];

//     $urlString = http_build_url($urlArray);
//     echo($urlString);

//php is messing up the line endings, so using a system call
 return  `curl '$urlString'`;
唯憾梦倾城 2024-11-16 11:33:08

您可以尝试使用 cURL 适配器设置 Zend_Http_Client 吗:

$client->setAdapter(new Zend_Http_Client_Adapter_Curl());

另外,您确定您没有在浏览器中显示 $response->getRawBody() 吗?浏览器将其解释为 HTML,因此将换行符解释为空格?
如果您右键单击-> 显示来源,你有换行符吗?

Can you try to setup Zend_Http_Client with the cURL adapter:

$client->setAdapter(new Zend_Http_Client_Adapter_Curl());

Also, are you sure you're not displaying $response->getRawBody() in your browser, which interprets it as HTML, therefore interpreting newlines as spaces?
If you right click -> show source, do you have the newlines?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文