简体   繁体   English

使用PHP中的服务器代理的HTTP POST请求

[英]HTTP POST Request using a server proxy in PHP

I'm trying to do an HTTP post request. 我正在尝试执行HTTP发布请求。 My server is running a php script with code here 我的服务器正在使用代码运行php脚本

<?
$siteurl = $_REQUEST['address'];
$postdata = '';
$fakeua = 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.0; Trident/5.0)';

$ch = curl_init($siteurl);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_USERAGENT, $fakeua);
curl_setopt($ch, CURLOPT_TIMEOUT, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);

?>

And I'm calling this in my JS like this 我在我的JS中这样称呼它

$.ajax({
    url: 'http://localhost/server.php',
    type: 'POST',
    data: {
      address: urlToPost
    },
    success: function(response) {
        window.console.log(response);
   }
});

Is this correct? 这个对吗? Should this work? 应该行吗? It seems I'm getting a 200 OK from the request, but something isn't right because I'm the website I'm doing a POST request to isn't showing it. 似乎我从请求中获得了200 OK,但由于我是正在执行POST请求的网站而没有显示它,因此出现了某些错误。 I am forming my url through concatenation, which should be fine as long as a I am concatenating it right, right? 我是通过串联形成url的,只要将它串联起来就可以了,对吗? Or should the paramaters be passed in? 还是应该传递参数?

urlToPost += "&s=" + sessionID + "&a[0]=" + "Girls" + "&t[0]=" + "Laura" + "&i[0]=" + ts2 + "&o[0]=E" + "&r[0]=" + "&l[0]=" + "&b[0]=Album" + "&n[0]=" + "&m[0]=";

Test it out by posting to google and checking that you are getting results. 通过发布到Google并检查您是否获得结果来进行测试。 use the q= parameter to set a search term. 使用q=参数设置搜索词。

Also, a 1 second timeout is pretty short. 另外,1秒的超时时间很短。

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

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