Php 来自本地主机的curl post

Php 来自本地主机的curl post,php,post,curl,http-headers,Php,Post,Curl,Http Headers,我试图通过使用curl从FireFox的LiveHTTP Replay模拟http post。我相信远程站点有某种验证来检查请求来自何处。如果请求来自他们自己的域,那么就可以了。当我尝试运行一个php curl脚本时,我可以从Live HTTP头中看到我正在发出GET请求而不是POST。此外,主机(预计为www.aliexpress.com)已自动更改为localhost 如果我使用实时HTTP重播,它可以正常工作。因此,我复制了标题数据并尝试使用curl实现,但没有成功。例如: 我尝试通过以

我试图通过使用curl从FireFox的LiveHTTP Replay模拟http post。我相信远程站点有某种验证来检查请求来自何处。如果请求来自他们自己的域,那么就可以了。当我尝试运行一个php curl脚本时,我可以从Live HTTP头中看到我正在发出GET请求而不是POST。此外,主机(预计为www.aliexpress.com)已自动更改为localhost

如果我使用实时HTTP重播,它可以正常工作。因此,我复制了标题数据并尝试使用curl实现,但没有成功。例如:

我尝试通过以下方式实现原始标头(如上所述):

<?php
// create a new cURL resource

$fields = array(
                        'productId' => 523523529,
                        'standard' => '',
                        'quantity' => 8,
                        'country' => 'MY',
                        'company' => 'CPAM',
                        'cartfrom' => 'main_store',
                        'skuAttr' => ''
                );

foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');

echo $fields_string . "<br/>";

$ch = curl_init();

// set URL and other appropriate options
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Host: www.aliexpress.com",
                                           "Content-Type: application/x-www-form-urlencoded", 
                                           "Content-length: ". "93",
                                           "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
                                           "Accept-Language: en-US,en;q=0.5",
                                           "Accept-Encoding: gzip, deflate",
                                           "X-Requested-With: XMLHttpRequest"));                                                                        //proceeding with the login.

curl_setopt($ch, CURLOPT_URL, urlencode("http://www.aliexpress.com/cross-domain/shoppingcart/index.html"));
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");

//The encoded url below is referring to the login form for aliexpress.com
curl_setopt($ch, CURLOPT_REFERER, "http%3A%2F%2Fwww.aliexpress.com%2Fstore%2Fproduct%2FDual-sim-I9300-S3-MTK6589-quad-core-android-phone-1G-RAM-4G-ROM-4-7-inch%2F901666_1035163509.html%3FpromotionId%3D210526801");//This tells the server where were you directed from.

curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);

//curl_setopt($ch, CURLOPT_COOKIESESSION, true);//indicates that this is a new session, i assume this forces the server to assign a new session?
//curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);//follows the redirection that is supplied by the server
curl_setopt($ch, CURLOPT_HEADER, true);
//curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);//THIS IS VERY IMPORTANT! This one of the most common option that is used because this simply means that
                                               //the response from the server is returned as a string rather than output directly.
curl_setopt($ch, CURLOPT_UNRESTRICTED_AUTH, true);//This means to keep sending the login information(username and password) when there is a redirection

$str = curl_exec($ch);

// close cURL resource, and free up system resources
curl_close($ch);
?>

谢谢你的帮助

问候,,
Dexter

尝试将
CURLOPT_POST
设置为true


curl_setopt($ch,CURLOPT_POST,1)

我认为您应该稍微修改一下代码:

  • 设置
    CURLOPT_URL
  • CURLOPT_HTTPHEADER
  • 使用
    http\u build\u query
    生成您的
    $fields\u字符串
出于调试目的,我将
CURLOPT_RETURNTRANSFER
设置为
true
,然后var转储响应

我的工作代码

$fields = array(
                        'productId' => 523523529,
                        'standard' => '',
                        'quantity' => 8,
                        'country' => 'MY',
                        'company' => 'CPAM',
                        'cartfrom' => 'main_store',
                        'skuAttr' => ''
                );

$fields_string = http_build_query($fields);

echo $fields_string . "<br/>";

$ch = curl_init();

// set URL and other appropriate options
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/x-www-form-urlencoded", 
                                           "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
                                           "Accept-Language: en-US,en;q=0.5",
                                           "Accept-Encoding: gzip, deflate",
                                           "X-Requested-With: XMLHttpRequest"));                                                                        //proceeding with the login.

curl_setopt($ch, CURLOPT_URL, "http://www.aliexpress.com/cross-domain/shoppingcart/index.html");
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");

//The encoded url below is referring to the login form for aliexpress.com
curl_setopt($ch, CURLOPT_REFERER, "http://www.aliexpress.com/store/product/Dual-sim-I9300-S3-MTK6589-quad-core-android-phone-1G-RAM-4G-ROM-4-7-inch/901666_1035163509.html?promotionId=210526801");//This tells the server where were you directed from.

curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);

curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);//THIS IS VERY IMPORTANT! This one of the most common option that is used because this simply means that
                                               //the response from the server is returned as a string rather than output directly.
curl_setopt($ch, CURLOPT_UNRESTRICTED_AUTH, true);//This means to keep sending the login information(username and password) when there is a redirection

$str = curl_exec($ch);

var_dump($str);
var_dump(curl_error($ch));

// close cURL resource, and free up system resources
curl_close($ch);

我试过了。同样的问题。主机仍然指向localhost,并且该方法仍然是回复的GetTanks。实际上,我可以得到200响应,但我的问题是,在使用curl命令时,URL引用的是localhost,这导致了问题。一旦远程站点注意到这一点,它将不会继续表单提交。只是尝试了另一种方法。试图上传到我的在线网站托管,但仍然无法工作。主持人仍然指向我自己的网站地址。我知道curl将指向正在执行它的网站,但我认为有一种方法可以绕过它?请提供帮助?
$fields = array(
                        'productId' => 523523529,
                        'standard' => '',
                        'quantity' => 8,
                        'country' => 'MY',
                        'company' => 'CPAM',
                        'cartfrom' => 'main_store',
                        'skuAttr' => ''
                );

$fields_string = http_build_query($fields);

echo $fields_string . "<br/>";

$ch = curl_init();

// set URL and other appropriate options
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/x-www-form-urlencoded", 
                                           "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
                                           "Accept-Language: en-US,en;q=0.5",
                                           "Accept-Encoding: gzip, deflate",
                                           "X-Requested-With: XMLHttpRequest"));                                                                        //proceeding with the login.

curl_setopt($ch, CURLOPT_URL, "http://www.aliexpress.com/cross-domain/shoppingcart/index.html");
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");

//The encoded url below is referring to the login form for aliexpress.com
curl_setopt($ch, CURLOPT_REFERER, "http://www.aliexpress.com/store/product/Dual-sim-I9300-S3-MTK6589-quad-core-android-phone-1G-RAM-4G-ROM-4-7-inch/901666_1035163509.html?promotionId=210526801");//This tells the server where were you directed from.

curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);

curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);//THIS IS VERY IMPORTANT! This one of the most common option that is used because this simply means that
                                               //the response from the server is returned as a string rather than output directly.
curl_setopt($ch, CURLOPT_UNRESTRICTED_AUTH, true);//This means to keep sending the login information(username and password) when there is a redirection

$str = curl_exec($ch);

var_dump($str);
var_dump(curl_error($ch));

// close cURL resource, and free up system resources
curl_close($ch);
HTTP/1.1 200 OK
Date: Sun, 04 Aug 2013 11:51:31 GMT
Server: Apache
P3P: CP="CAO PSA OUR"
Content-Language: en-US
Vary: Accept-Encoding,User-Agent
Content-Encoding: gzip
X-XSS-protection: 1;mode=block
Content-Length: 56
Content-Type: plain/text;charset=utf-8
Set-Cookie: ali_apache_id=1.54.42.221.1375617091161.869918.6; path=/; domain=.aliexpress.com; expires=Wed, 30-Nov-2084 01:01:01 GMT
Set-Cookie: JSESSIONID=6EB1295945C27F8A2F788587D4C0E0A7; Path=/
Set-Cookie: ali_apache_track=; Domain=.aliexpress.com; Expires=Fri, 22-Aug-2081 15:05:38 GMT; Path=/
Set-Cookie: ali_apache_tracktmp=; Domain=.aliexpress.com; Path=/
Set-Cookie: acs_usuc_t=acs_rt=8fdfad47f53b46d489d0a905a5a9fb7c; Domain=.aliexpress.com; Path=/
Set-Cookie: xman_t=ZwO1ZDjGpaou2015+mejeWnS90vHjsN3YIDxbrXYOz/mbbJeIZM3q7Pw6ZGTygK2; Domain=.aliexpress.com; Path=/; HttpOnly
Set-Cookie: acs_t=2nqPb5i+QB7aDai5FXRM12xDJghxP4qjmcwPjwaXQ4SI6eV7eGpxjRGNjukEXuEW; Domain=.aliexpress.com; Path=/; HttpOnly
Set-Cookie: xman_f=MC/MUpjkYCKP+PRcAK43k9eQrTR+PE1rldMoChEUHVVlAUcYwh10BKJ0lxWlsPe4p+pYIPC/Vy4wIHJK8fiy4koUaF68CAolRC6UH7q0nmU5HcqWzgyjnA==; Domain=.aliexpress.com; Expires=Fri, 22-Aug-2081 15:05:38 GMT; Path=/; HttpOnly