简体   繁体   English

cURL 显示空白页

[英]cURL shows blank page

my curl code show me a blank page我的 curl 代码显示一个空白页

 <?
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_URL,"http://mysite/scripts/showsomething.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$result=curl_exec ($ch);
curl_close ($ch);
$core = explode('<!--- BEGIN -->', $result); 
$main = explode('<!--- END -->', $core[1]);
echo $main[0]; 
?>

this code works fine on localhost, but not on server...这段代码在本地主机上工作正常,但在服务器上不起作用......

There can be several reasons behind your problem.您的问题背后可能有多种原因。

1) Change <? 1) 改变<? into <?php and see whether it works or not.进入<?php看看它是否有效。

2) For a short test, run this code from your server and check whether it shows you the output or not. 2) 对于简短的测试,从您的服务器运行此代码并检查它是否显示输出。

<?php
  echo "sabuj";
?>

3) Some site seek for useragent string on their website request. 3) 某些站点在其网站请求中寻找用户代理字符串。 When they found no useragent, they use to return blank page.当他们找不到用户代理时,他们使用返回空白页。 You can overcome this with setting an useragent like below.您可以通过设置如下所示的用户代理来克服这个问题。

curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0');

4) Another thing you can do is, access the server with ssh client(if you have any) and run the url with wget tool and see whether you can download your page or not. 4)您可以做的另一件事是,使用ssh客户端(如果有)访问服务器并使用wget工具运行 url,看看您是否可以下载您的页面。

wget "http://yoursite/page/blabla/...php"

5) Finally, run your curl code with verbose mode enabled. 5) 最后,在启用详细模式的情况下运行 curl 代码。 It will help you to debug your curl requests.它将帮助您调试 curl 请求。

curl_setopt($ch, CURLOPT_VERBOSE,true);

Working Code...工作代码...

       $ch = curl_init();
       $timeout = 10;
       curl_setopt($ch, CURLOPT_URL, $url);
       curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
       curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
       $data = curl_exec($ch);
       curl_close($ch);
       var_dump($data);

Hope it helps...希望能帮助到你...

Turn your error reporting all the way to 11.将您的错误报告一直设置为 11。

error_reporting(E_ALL);

This will tell you a little more about the issue you are facing.这将告诉您更多有关您面临的问题的信息。 The most common culprit when working with curl on a dev environment and moving to a server is a missing curl package.在开发环境中使用 curl 并移动到服务器时,最常见的罪魁祸首是缺少 curl 包。

You can check to see if you have curl installed by doing the following:您可以通过执行以下操作来检查是否安装了 curl:

if(!function_exists('curl_version')) {
    throw new Exception('Curl package missing');
}

Thus "PHP Fatal error: Call to undefined function curl_init() in..." is a very common error that is thrown.因此,“PHP Fatal error: Call to undefined function curl_init() in...”是一个非常常见的错误。

Some additional debugging tips are to..一些额外的调试技巧是...

  1. print_r the response of curl_exec($handle); print_r curl_exec($handle) 的响应;
  2. print_r curl_error($handle) this will give you a curl error code. print_r curl_error($handle) 这会给你一个 curl 错误代码。
  3. setup a curl proxy using set_opt and CURLOPT_PROXY, set the value to your ip:port, open the port 8888 on your router and install charles proxy.使用 set_opt 和 CURLOPT_PROXY 设置 curl 代理,将值设置为您的 ip:port,打开路由器上的端口 8888 并安装 charles 代理。 This will show you an exact printout of the request and response.这将显示请求和响应的准确打印输出。

I've read the previous answers and it seems that your code is fine but problem is connectivity to remote server, please try this:我已经阅读了以前的答案,似乎您的代码很好,但问题是与远程服务器的连接,请尝试以下操作:

<?
//debug
error_reporting(E_ALL);
ini_set('display_errors', 1);
//debug end

$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_URL,"http://mysite/scripts/showsomething.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$result=curl_exec ($ch);
curl_close ($ch);
echo result;
?>

what does it output ?它输出什么?

Notes:笔记:

Do you administer the other server?您管理其他服务器吗?
if not, is there a possibility that your ip was blocked by the remote server?如果没有,是否有可能您的 ip 被远程服务器阻止了?
Does script http://mysite/scripts/showsomething.php contains any errors ?脚本http://mysite/scripts/showsomething.php是否包含任何错误?
If you're able to edit it, please enable php errors on showsomething.php by adding the following code at the top of it :如果你能对其进行编辑,请启用PHP错误showsomething.php通过在它的顶部加入如下代码:

error_reporting(E_ALL);
ini_set('display_errors', 1);

I've solved the problem by adding the following code.我通过添加以下代码解决了这个问题。 The problem is SSL.问题是 SSL。

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

I hope it helps.我希望它有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM