curl通过代理返回没有内容 [英] curl through proxy returns no content

查看:283
本文介绍了curl通过代理返回没有内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个PHP脚本,它发送请求到我们学校的服务器,获得关于不同课程的班级规模的实时信息。当我不使用代理,返回一个字符串充满了当然数字和可用的座位,脚本运行完美。但是,我想让这是一个服务的学生,我恐怕,如果我提出太多的请求,我的ip将被阻止。所以我试图通过代理这样做,没有成功。一旦我添加CURLOPT_HTTPPROXYTUNNEL和CURLOPT_PROXY字段到我的请求,没有什么返回。我甚至不知道如何解决它在这一点,因为我没有得到任何类型的错误消息。有没有人知道发生了什么或如何至少解决它?

I'm working on a PHP script right now which sends requests to our school's servers to get real-time information about class sizes for different courses. The script runs perfectly fine when I don't use a proxy, returning a string full of course numbers and available seats. However, I want to make this a service for the students, and I'm afraid that if I make too many requests my ip will get blocked. So I'm attempting to do this through a proxy, with no success. As soon as I add the CURLOPT_HTTPPROXYTUNNEL and CURLOPT_PROXY fields to my requests nothing gets returned. I'm not even sure how to troubleshoot it at this point since I'm not getting an error message of any kind. Does anyone know what's going on or how to at least troubleshoot it?

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$proxy = explode(':', $proxy);
curl_setopt($ch, CURLOPT_PROXY, $proxy[0]);
curl_setopt($ch, CURLOPT_PROXYPORT, $proxy[1]);
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, 'tempcookie.txt');
curl_setopt($ch, CURLOPT_COOKIEJAR, 'tempcookie.txt');
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_REFERER, $ref);
$exec = curl_exec($ch);

echo curl_error($ch);
print_r(curl_getinfo($ch));
echo $exec;

用于测试的代理: 75.147.173.215:8080

推荐答案

如果我必须使用curl的代理,我使用下面的代码:

I use the following code if I have to use a proxy with curl:

$proxy = "127.0.0.1:8080"; // or something like that

if($proxy !== null){

    // no need to specify PROXYPORT again
    curl_setopt($ch, CURLOPT_PROXY, $proxy);

    // to make the request go through as though proxy didn't exist
    curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 0);

}

这篇关于curl通过代理返回没有内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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