Ricevo la richiesta POST sul mio script PHP e vorrei inoltrare questa post call a un altro script usando anche il POST. Come fare questo? Posso usare cURL se è richiesto per questa azione.
risposta
Forse:
curl_setopt($ch, CURLOPT_POSTFIELDS, $_POST);
http://www.php.net/manual/en/function.curl-setopt.php
Questo può essere passato come stringa urlencoded come 'para1 = val1 & para2 = val2 & ...' o come un array con il nome del campo come chiave e dati sul campo come valore.
Hai mai provato prima? Con la versione di CURL che utilizzo, invierò i campi in "multipart/form-data", non in un post normale. –
Non l'ho mai provato prima, ma la documentazione di PHP fa un buon lavoro di documentazione della funzionalità. –
Questa è la risposta più delle volte. Tuttavia, se nel contenuto del post sono passate variabili profonde (ad es. "... & var1 [var2] = val & ...") non funzionerà ('var1' verrà passato come array vuoto). La risposta di ZZCoder qui sotto (usando 'http_build_query()') è la risposta giusta (completa). – zeh
<?php
function executeCurl($arrOptions) {
$mixCH = curl_init();
foreach ($arrOptions as $strCurlOpt => $mixCurlOptValue) {
curl_setopt($mixCH, $strCurlOpt, $mixCurlOptValue);
}
$mixResponse = curl_exec($mixCH);
curl_close($mixCH);
return $mixResponse;
}
// if need any http auth
$username = 'http-auth-username';
$password = 'http-auth-password';
$requestType = 'POST'; // this can be PUT or POST
// this can be $arrPostData = $_POST;
$arrPostData = array(
'key1' => 'value-1-for-k1y-1',
'key2' => 'value-2-for-key-2',
'key3' => array(
'key31' => 'value-for-key-3-1',
'key32' => array(
'key321' => 'value-for-key321'
)
),
'key4' => array(
'key' => 'value'
)
);
// you can set your post data
$postData = http_build_query($arrPostData); // raw php array
$postData = json_encode($arrPostData); // Only USE this when request json data
$arrResponse = executeCurl(array(
CURLOPT_URL => 'http://whatever-your-request-url.com/xyz/yii',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPGET => true,
CURLOPT_VERBOSE => true,
CURLOPT_AUTOREFERER => true,
CURLOPT_CUSTOMREQUEST => $requestType,
CURLOPT_POSTFIELDS => $postData,
CURLOPT_HTTPHEADER => array(
"X-HTTP-Method-Override: " . $requestType,
'Content-Type: application/json', // Only USE this when request json data
),
// if required HTTP Authentication use below lines
CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
CURLOPT_USERPWD => $username. ':' . $password
));
Se qualcuno ha bisogno di questo, ecco una richiesta CURL completamente funzionale che ri-rotte $ _POST in cui si desidera (sulla base risposta ZZ del coder sopra)
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://urlOfFileWherePostIsSubmitted.com");
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
// ZZ coder's part
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($_POST));
$response = curl_exec($ch);
curl_close($ch);
- 1. Come utilizzare OAuth con PHP e cURL?
- 2. come spoof referrer con curl e php?
- 3. chiamata php con cURL in windows azure
- 4. API Paypal con PHP e cURL
- 5. vbulletin post con CURL e PHP
- 6. PHP: $ _GET e $ _POST in funzioni?
- 7. PHP $ _POST non funziona?
- 8. Php Curl invia file E dati array
- 9. PHP cURL asincrono con callback
- 10. PHP $ _POST recupera matrice dati
- 11. come caricare file utilizzando curl con php
- 12. PHP Curl con bandiera --data?
- 13. debug e ispezionare $ _POST
- 14. Neteller TransferOut con PHP/CURL
- 15. Polling lungo con curl php
- 16. Echo an $ _POST in PHP
- 17. PHP: Come sostituire in massa $ _POST [...] con strip_tags ($ _ POST [...])
- 18. Come posso effettuare una richiesta con entrambi i parametri GET e POST in PHP con cURL?
- 19. Come pubblicare i dati JSON con PHP cURL?
- 20. PHP, CURL. Cosa restituisce curl_exec?
- 21. Ottenere $ _POST con Joomla 2.5
- 22. Salvare il file usando curl e PHP
- 23. Differenza PHP tra Curl e HttpRequest
- 24. Accedi a Google con PHP e Curl, Cookie disattivato?
- 25. PHP Curl and Cookies
- 26. Richiesta SOAP in PHP con CURL
- 27. Image upload CURL in PHP Curl
- 28. PHP Curl post con caratteri non inglesi
- 29. CURL PHP invia immagine
- 30. Intestazioni personalizzate PHP cURL
Il tuo script PHP ha bisogno di accedere alla risposta inviata dal POST inoltrato? – webbiedave