Beispielcode
<code><span>$ch</span> = curl_init(); curl_setopt_array(<span>$ch</span>, [ CURLOPT_RETURNTRANSFER=><span>true</span>, CURLOPT_POST=><span>true</span>, CURLOPT_HEADER=><span>false</span>, CURLOPT_TIMEOUT=><span>30</span>, CURLOPT_HTTPHEADER=>[<span>"Content-type:application/json"</span>], CURLOPT_URL=><span>"http://localhost/city"</span>, CURLOPT_POSTFIELDS=><span>'{"province_id":39}'</span>, ]); <span>$result</span> = curl_exec(<span>$ch</span>); print_r(<span>$result</span>);</code>
Rückgabeergebnis:
<code>{"<span>code</span>":<span><span>1</span></span>,"<span>data</span>":<span>[{"<span>id</span>":<span><span>"40"</span></span>,"<span>name</span>":<span><span>"石家庄市"</span></span>},{"<span>id</span>":<span><span>"64"</span></span>,"<span>name</span>":<span><span>"唐山市"</span></span>},{"<span>id</span>":<span><span>"80"</span></span>,"<span>name</span>":<span><span>"秦皇岛市"</span></span>},{"<span>id</span>":<span><span>"89"</span></span>,"<span>name</span>":<span><span>"邯郸市"</span></span>},{"<span>id</span>":<span><span>"110"</span></span>,"<span>name</span>":<span><span>"邢台市"</span></span>},{"<span>id</span>":<span><span>"131"</span></span>,"<span>name</span>":<span><span>"保定市"</span></span>},{"<span>id</span>":<span><span>"158"</span></span>,"<span>name</span>":<span><span>"张家口市"</span></span>},{"<span>id</span>":<span><span>"177"</span></span>,"<span>name</span>":<span><span>"承德市"</span></span>},{"<span>id</span>":<span><span>"190"</span></span>,"<span>name</span>":<span><span>"沧州市"</span></span>},{"<span>id</span>":<span><span>"208"</span></span>,"<span>name</span>":<span><span>"廊坊市"</span></span>},{"<span>id</span>":<span><span>"220"</span></span>,"<span>name</span>":<span><span>"衡水市"</span></span>}]</span>}</code>
Erklärung:
').addClass('pre-numbering ').verstecken(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });Der Standardwert von CURLOPT_HEADER ist falsch, also der oben Die Option kann deaktiviert bleiben
Wenn Sie keine Daten im JSON-Format übergeben, können Sie http_build_query verwenden, um den Wert von CURLOPT_POSTFIELDS zu erstellen (in der Form ähnlich wie name=tom&age=25)
Das Obige stellt die Verwendung der PHP-Curl-Erweiterung zum Übermitteln von Daten im JSON-Format vor, einschließlich Aspekten des Inhalts. Ich hoffe, dass es für Freunde hilfreich sein wird, die an PHP-Tutorials interessiert sind.