0

送信ボタンに名前属性がないcurlを使用してフォームを送信するにはどうすればよいですか?

たとえば、ターゲットサイトには次の送信ボタンがあります。

<input id='some' value='value' type='submit' >

これは私が持っているものです:

$post_data['name'] = 'xyz';
$post_data['some'] = 'value';

foreach ( $post_data as $key => $value) {
            $post_items[] = $key . '=' . $value;
        }
        //create the final string to be posted using implode()
        $post_string = implode ('&', $post_items);

    $ch = curl_init($web3);
        //set options
        //curl_setopt($ch, CURLOPT_COOKIESESSION, true);
        curl_setopt ($ch, CURLOPT_COOKIEFILE, $ckfile); 
        curl_setopt ($ch, CURLOPT_COOKIEJAR, $ckfile); 
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
        curl_setopt($ch, CURLOPT_USERAGENT,
          "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
        //curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        //set data to be posted
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
        //perform our request
        $result = curl_exec($ch);
4

2 に答える 2

3

(通常) をエンコードする必要はありません。$post_data以下のコードはフォーム送信します。

ボタンは不要です。実行するcurl_execと、サーバーは入力されたフォームと同等のものを受け取ります (post_data正しい場合)。

アクションが進行しない場合は、一部のフィールドが欠落しているか、セッションに何かが欠落している可能性があります。フォームを表示する同じページを cURL で開いてから、フォームを送信してみてください。

$post_data['name'] = 'xyz';
$post_data['some'] = 'value';

$ch = curl_init();
//set options
//curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt ($ch, CURLOPT_COOKIEFILE, $ckfile); 
curl_setopt ($ch, CURLOPT_COOKIEJAR, $ckfile); 
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
    curl_setopt($ch, CURLOPT_USERAGENT,
      "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
//curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

//set data to be posted
curl_setopt($ch,CURLOPT_POST, true);

// Note -- this will encode using www-form-data
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);

curl_setopt($ch, CURLOPT_URL, $web3);

$result = curl_exec($ch);

アップデート

では、通常はどうなるか、cURL ではどうなるか見てみましょう。

   Browser                          cURL
1. Goes to www.xyz.com/form         curl_exec's a GET on www.xyz.com/form
2. Server sends HTML                Server sends HTML
3. User types in fields             We populate $post_data
4. User clicks "SUBMIT"             We run curl_exec and POST $post_data
5. Browser contacts server          cURL contacts server
6. Browser sends fields             cURL sends fields
7. Server acts upon request         Server acts upon request
8. Profit                           Profit

上記のコードは、フェーズ 3 ~ 6 のみを実装しています。フェーズ 2 でセッション Cookie を送信し、フェーズ 7 で必要なデータを設定することもできます。そのため、別の curl_exec (おそらく今回は GET メソッド) を使用してフェーズ 1 を実装する必要があります。次のフェーズを実行します。

于 2012-08-30T21:28:20.930 に答える
2

その入力フォームフィールドの名前を入力しないでください。ブラウザが実際に実行する名前です。

于 2012-08-30T20:27:26.290 に答える