cURL提交POST数据失败


cURL failing to submit POST data

我想提交一个简单的表单与cURL。在获得登录cookie并提交我想要的数据后,我得到一个随机响应,表单提交失败。下面是浏览器提交表单时的样子:

formPost:cur_product_form_new_86286
curCheckbox:Y
C2cProductsListing[business_hr_from]:10:00:00
C2cProductsListing[business_hr_to]:09:30:00
C2cProductsListing[online_hr]:35
C2cProductsListing[offline_hr]:16
C2cProductsListing[non_business_hr]:17
C2cProductsListing[actual_quantity]:25000
C2cProductsListing[minimum_quantity]:25000
C2cProductsListing[products_base_currency]:USD
C2cProductsListing[products_price]:88
delivery[1]:1
C2cProductsListing[c2c_products_listing_id]:86286
下面是我提交表单时的样子:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.g2g.com/sell/manageListingInfo'); // open a protected page
curl_setopt($ch, CURLOPT_REFERER, 'http://www.g2g.com/sell/manageListing?game=2522&product_type=19248');
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.A.B.C Safari/525.13");
$data = array(
    'formPost' => 'cur_product_form_new_86286',
    'curCheckbox' => 'Y',
    'C2cProductsListing' => array(
        'business_hr_from' => '09:00:00',
        'business_hr_to' => '08:30:00',
        'online_hr' => '35',
        'offline_hr' => '16',
        'non_business_hr' => '17',
        'actual_quantity' => '25000',
        'minimum_quantity' => '25000',
        'products_base_currency' => 'USD',
        'products_price' => '25',
        'c2c_products_listing_id' => '86286'
    ),
    'delivery' => array(
        '1' => '1'
    )
);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_REFERER, 'http://www.g2g.com/sell/manageListing?game=2522&product_type=19248');
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_FAILONERROR, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
print_r(curl_getinfo($ch));
curl_close($ch);

这是我得到的完全随机的回答:

Array ( [url] => http://www.g2g.com/sell/manageListingInfo [content_type] => [http_code] => 0 [header_size] => 0 [request_size] => 0 [filetime] => 0 [ssl_verify_result] => 0 [redirect_count] => 0 [total_time] => 0 [namelookup_time] => 0 [connect_time] => 0 [pretransfer_time] => 0 [size_upload] => 0 [size_download] => 0 [speed_download] => 0 [speed_upload] => 0 [download_content_length] => -1 [upload_content_length] => -1 [starttransfer_time] => 0 [redirect_time] => 0 [certinfo] => Array ( ) )

我刚刚发布的数组是print_r(curl_getinfo($ch))的结果。我是怎么到这里来的?我完全模拟了浏览器请求(除了它是cURL而不是实际的浏览器),使用了所有必要的数据,但表单似乎没有提交?

您没有调用curl_exec来实际执行查询。之后呼叫curl_getinfo。如果您希望返回数据,请使用curl_exec而不是curl_getinfo的返回值。例如:

$data = curl_exec($ch);
echo $data;