使用Php Curl发布数组数据

时间:2014-02-21 22:47:33

标签: php curl

我有以下标题:

Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Cache-Control:max-age=0
Connection:keep-alive
Content-Length:3371
Content-Type:multipart/form-data; boundary=----WebKitFormBoundaryQclrmH4Iax0ZaEMA
........
------WebKitFormBoundaryQclrmH4Iax0ZaEMA
Content-Disposition: form-data; name="postcode"

375857
------WebKitFormBoundaryQclrmH4Iax0ZaEMA
Content-Disposition: form-data; name="area"


------WebKitFormBoundaryQclrmH4Iax0ZaEMA
Content-Disposition: form-data; name="visibleOnMap"

true
------WebKitFormBoundaryQclrmH4Iax0ZaEMA
Content-Disposition: form-data; name="title"

title here
------WebKitFormBoundaryQclrmH4Iax0ZaEMA
Content-Disposition: form-data; name="attributes['price']"

379.99
------WebKitFormBoundaryQclrmH4Iax0ZaEMA
Content-Disposition: form-data; name="file"; filename=""
Content-Type: application/octet-stream

------WebKitFormBoundaryQclrmH4Iax0ZaEMA
Content-Disposition: form-data; name="description"

===== Description here ===== 
------WebKitFormBoundaryQclrmH4Iax0ZaEMA
Content-Disposition: form-data; name="websiteUrl"


------WebKitFormBoundaryQclrmH4Iax0ZaEMA
Content-Disposition: form-data; name="features['URGENT'].productName"

URGENT
------WebKitFormBoundaryQclrmH4Iax0ZaEMA
Content-Disposition: form-data; name="features['FEATURED'].productName"

FEATURE_7_DAY
------WebKitFormBoundaryQclrmH4Iax0ZaEMA
Content-Disposition: form-data; name="features['SPOTLIGHT'].productName"

HOMEPAGE_SPOTLIGHT
------WebKitFormBoundaryQclrmH4Iax0ZaEMA
Content-Disposition: form-data; name="features['WEBSITE_URL'].productName"

WEBSITE_URL
------WebKitFormBoundaryQclrmH4Iax0ZaEMA
Content-Disposition: form-data; name="contactEmail"

email@mail.com
------WebKitFormBoundaryQclrmH4Iax0ZaEMA
Content-Disposition: form-data; name="usePhone"

true
------WebKitFormBoundaryQclrmH4Iax0ZaEMA
Content-Disposition: form-data; name="contactTelephone"

0012345678
------WebKitFormBoundaryQclrmH4Iax0ZaEMA
Content-Disposition: form-data; name="contactName"

Dikl
------WebKitFormBoundaryQclrmH4Iax0ZaEMA
Content-Disposition: form-data; name="submit-new"

Post
------WebKitFormBoundaryQclrmH4Iax0ZaEMA--

我尝试使用以下PHP curl调用,但没有成功。:

$post = array(
            'postcode'=>$postcode,
            'area' => "",
            'visibleOnMap' => "true",
            'title' => $title,
            'attributes[\'price\']' => $price,
            'mainImageId' => $images[0]['id'],
            'file"; filename=""' => "",
            'youtubeLink' => "",
            'description' => $description,
            'websiteUrl' => '',
            'features[\'URGENT\'].productName' => 'URGENT',
            'features[\'FEATURED\'].productName' => 'FEATURED',
            'features[\'SPOTLIGHT\'].productName' => 'SPOTLIGHT',
            'features[\'WEBSITE_URL\'].productName' => 'WEBSITE_URL',
            'contactEmail' => $contactEmail,
            'usePhone' => 'true',
            'contactTelephone' => $contactTelephone,
            'contactName' => $contactName,
            'submit-new' => 'Post my ad',
            );
$page = curl($url,$post,$cookiefile);
function curl($link, $postfields = '', $cookie = '', $refer = '',$header=true)
{
    $ch = curl_init($link);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
   if($header)
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER,array("Expect:"));
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1');
    if ($refer) {
        curl_setopt($ch, CURLOPT_REFERER, $refer);
    }
    if ($postfields) {
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
    }
    curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
    curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    $page = curl_exec($ch);
    if($page === false)
    {
        echo 'Curl error: ' . curl_error($ch);
    }   
    curl_close($ch);
    return $page;
}

我不确定如何传递像属性['price']或功能['WEBSITE_URL']这样的数组.productName等...是将数据传递给curl post post的正确方法吗?

0 个答案:

没有答案