2010-11-24 6 views
10

J'ai besoin de POSTER le code JSON suivant, mais pour une raison quelconque, il ne fonctionne pas. Voici le code que j'ai.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; 
} 

Voici la fonction qui appelle la demande cURL:

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

Donnez à json_encode un tableau PHP, pas une chaîne qui est déjà sous la forme d'un objet JS. – JAL

+0

J'ai essayé curl_setopt ($ ch, CURLOPT_POSTFIELDS, tableau (json_encode (array ( "categoryId" => "5016")))); et json_encode (array ( "categoryId" => "5016"))); et ne fonctionne pas non plus – Michael

+3

Il est super ennuyeux que vous avez apparemment résolu le problème (comme indiqué par votre commentaire), mais n'a pas mis à jour le poste pour indiquer quelle était la solution. – shanusmagnus

Répondre

16

Le bit qui est le problème:

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

CURLOPT_POSTFIELDS acceptent soit un tableau de paramètres, ou un Chaîne de paramètres codée en URL:

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

json sera le nom du champ POST (c'est-à-dire que $_POST['json'] sera accessible).

+0

pour certaines raisons ne fonctionne pas. Je reçois {"Message": "Il y avait une erreur de traitement de la requête.", "StackTrace": "", "ExceptionType": ""} Array ([Message] => Une erreur est survenue lors du traitement de la requête. StackTrace] => [ExceptionType] =>). Je pense que c'est parce que le poste n'est pas fait correctement. – Michael

+0

J'ai téléchargé une image sur ce lien afin que vous puissiez voir exactement la demande post de http analyseur que je suis en train de reproduire avec php curl. http://img502.imageshack.us/img502/1346/analyzer.jpg – Michael

+1

comme vous pouvez le voir dans l'analyseur http n'apparaissent pas de champ de message (par exemple "json") – Michael

8

Envoyer votre texte sans codage JSON et ajoutez ce code d'en-tête:

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

j'eu des problèmes d'envoi JSON via cURL, et le problème était que je ne déclenchaient pas explicitement la longueur du contenu dans l'en-tête.

Ainsi l'en-tête doit être:

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

Avec l'exemple initial, le code de travail devrait ressembler à ceci:

//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

Il est assez simple en fait, assurez-vous que vous avez défini le contenu supplémentaire -Type d'en-tête défini pour json, puis CURLOPT_POSTFIELDS peut prendre le json sous forme de chaîne. Aucun encodage nécessaire.