PHP cURL表示在向虚拟主机发送邮件时永久移动 [英] PHP cURL says Moved Permanently when POSTing to a virtual host

查看:124
本文介绍了PHP cURL表示在向虚拟主机发送邮件时永久移动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用PHP cURL将一些数据发布到使用虚拟主机来拥有自定义域 http://example.local 的本地网站,但结果似乎是永久移动



这是我目前的代码:

  $ url =http://example.local/paypal_ipn.php
$ ch = curl_init();
//设置url,POST变量的数量,POST数据
curl_setopt($ ch,CURLOPT_URL,$ url);

curl_setopt($ ch,CURLOPT_POST,substr_count($ req,'&')+ 1);
curl_setopt($ ch,CURLOPT_POSTFIELDS,$ req);

//执行post
$ result = curl_exec($ ch);

//关闭连接
curl_close($ ch);

我试图将 CURLOPT_FOLLOWLOCATION

使用 CURLOPT_CUSTOMREQUEST curl_setopt($ curl,CURLOPT_CUSTOMREQUEST,CURLOPT_CUSTOMREQUEST),而不是 CURLOPT_POST POST);

再一次,您需要添加

  curl_setopt($ cur1,CURLOPT_POSTREDIR,3); 

3表示对301和302使用相同类型的请求重定向。



通过这样做,第二个请求也将作为POST请求。注意,在PHP 5.3.2中实现 CURLOPT_POSTREDIR 这里


I am trying to POST some data with PHP cURL to a local site that's using virtual hosts to have a custom domain http://example.local, but the result seems to be a Moved Permanently. How could I get this to work?

This is my current code:

$url = "http://example.local/paypal_ipn.php"
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);

curl_setopt($ch,CURLOPT_POST, substr_count($req,'&')+1);
curl_setopt($ch,CURLOPT_POSTFIELDS, $req);

//execute post
$result = curl_exec($ch);

//close connection
curl_close($ch);

I've tried to set CURLOPT_FOLLOWLOCATION as true, but that just follows without the POST data.

解决方案

Use CURLOPT_CUSTOMREQUEST instead of CURLOPT_POST like;

curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");

And one more thhing, you need to add

curl_setopt($curl, CURLOPT_POSTREDIR, 3);

3 means follow redirect with the same type of request both for 301 and 302 redirects.

By doing this, second request will be as POST request as well. Note that, CURLOPT_POSTREDIR implemented in PHP 5.3.2 here

这篇关于PHP cURL表示在向虚拟主机发送邮件时永久移动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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