2010-11-24 34 views
10

aşağıdaki JSON kodu POST gerekiyor, ama nedense çalışmıyor. Aşağıda sahip olduğum kod. İşte PHP cURL, POST JSON

$fieldString = "395609399"; 
//the curl request processor 
function processCurlJsonrequest($URL, $fieldString) { //Initiate cURL request and send back the result 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_HTTPHEADERS, array('Content-Type: application/json')); 
    curl_setopt($ch, CURLOPT_URL, $URL); 
    curl_setopt($ch, CURLOPT_USERAGENT, $this->_agent); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); 
    curl_setopt($ch, CURLOPT_COOKIEFILE, $this->_cookie_file_path); 
    curl_setopt($ch, CURLOPT_COOKIEJAR, $this->_cookie_file_path); 
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE); 
    curl_setopt($ch, CURLOPT_VERBOSE, TRUE); 
    if ($fieldCount) { // in case of post fields present then pass it on 
     curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode("{categoryId: $fieldString}")); 
     curl_setopt($ch, CURLOPT_POST, 1); 
    } 
    $resulta = curl_exec($ch); 
    if (curl_errno($ch)) { 
     print curl_error($ch); 
    } else { 
     curl_close($ch); 
    } 
    return $resulta; 
} 

cURL isteği çağıran fonksiyonudur:

function get_cat($categoryId, $URL) { 
    $fields = array(
     "categoryId" => $categoryId 
    ); 
    $fields_string = $fields; 
    return $this->processCurlJsonrequest($URL, $fields_string); 
} 
+0

bir PHP dizi, bir JS nesnesi biçiminde zaten olmayan bir dize json_encode verin. – JAL

+0

denedim curl_setopt ($ ch, CURLOPT_POSTFIELDS, array (json_encode (dizi ( "CategoryID" => "5016")))); ve json_encode (dizi ( "categoryId" => "5016"))); ve çalışmıyor ya – Michael

+3

O (Yorumlarınız ile belirtildiği gibi) ama ne çözüm oldu belirtmek için yazı güncelleme vermedi Eğer görünüşte sorun çözüldü süper sinir bozucu. Bazı nedenlerle – shanusmagnus

cevap

16

sorundur biraz:

curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode("{categoryId: $fieldString}")); 

ya parametrelerin bir dizi kabul eder CURLOPT_POSTFIELDS veya parametrelerin URL olarak kodlanmış dize:

curl_setopt($ch, CURLOPT_POSTFIELDS, array('json'=>json_encode($stuff))); 
curl_setopt($ch, CURLOPT_POSTFIELDS, 'json='.urlencode(json_encode($stuff))); 

json, POST alanının adı olacaktır (yani: $_POST['json'] erişilebilir duruma gelir).

+0

çalışmıyor. {"İleti" alıyorum: "İsteğin işlenmesi sırasında bir hata oluştu.", "StackTrace": "", "ExceptionType": ""} Array ([Message] => İstek işlenirken bir hata oluştu. StackTrace] => [ExceptionType] =>). Sanırım yazı düzgün yapılmadığı için. – Michael

+0

Bu bağlantıya bir resim yükledim, böylece php curl ile çoğaltmaya çalıştığım http analizöründen tam olarak gönderilen isteği görebilirsiniz. http://img502.imageshack.us/img502/1346/analyzer.jpg – Michael

+1

herhangi sonrası alanını (örneğin "json") – Michael

8

JSON kodlama olmadan kısa mesaj gönder, ve bu başlık kodu ekleyin:

curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json; charset=utf-8","Accept:application/json, text/javascript, */*; q=0.01")); 
3

Ben cURL aracılığıyla JSON gönderme sorunları ve sorun açıkça başlığındaki içerik uzunluğunu ayarlama değil olmasıydı.

Yani Başlık olmalıdır: İlk örnek ile

curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Content-Length: ' . strlen($json))); 
6

, böyle olmalı, çalışan kod:

//the curl request processor 
function processCurlJsonrequest($URL, $fieldString) { //Initiate cURL request and send back the result 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); 
    curl_setopt($ch, CURLOPT_URL, $URL); 
    curl_setopt($ch, CURLOPT_USERAGENT, $this->_agent); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); 
    curl_setopt($ch, CURLOPT_COOKIEFILE, $this->_cookie_file_path); 
    curl_setopt($ch, CURLOPT_COOKIEJAR, $this->_cookie_file_path); 
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE); 
    curl_setopt($ch, CURLOPT_VERBOSE, TRUE); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(array("myJsonData" => "test"))); 
    curl_setopt($ch, CURLOPT_POST, 1); 
    $resulta = curl_exec($ch); 
    if (curl_errno($ch)) { 
     print curl_error($ch); 
    } else { 
     curl_close($ch); 
    } 
    return $resulta; 
} 
2

Gerçekten çok basit, emin olun size ekstra İçerik kurdum -Type header json için ayarlanmış ve ardından CURLOPT_POSTFIELDS jsonu bir string olarak alabilir. Kodlama gerekli değil.