2010-04-27 23 views
9

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.

+0

Il tuo script PHP ha bisogno di accedere alla risposta inviata dal POST inoltrato? – webbiedave

risposta

12

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.
+0

Hai mai provato prima? Con la versione di CURL che utilizzo, invierò i campi in "multipart/form-data", non in un post normale. –

+0

Non l'ho mai provato prima, ma la documentazione di PHP fa un buon lavoro di documentazione della funzionalità. –

+1

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

12

Fate questo,

curl_setopt($handle, CURLOPT_POSTFIELDS, http_build_query($_POST)); 
+0

Questo ha fatto il trucco! Grazie. http_build_query() è necessario, altrimenti non funzionerà. – acme

+0

Questa è la risposta corretta – bonez

0
<?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 
)); 
1

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);