0

次のコードがあります。

$poststr = "param1=<html><head></head><body>test1 & test2</body></html>&param2=abcd&param3=eeee";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "http://www.mytest.com");
curl_setopt($curl, CURLOPT_COOKIEFILE, $cookiefile);
curl_setopt($curl, CURLOPT_COOKIEJAR, $cookiefile);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $poststr);
curl_setopt($curl, CURLOPT_ENCODING, "");
$curlData = curl_exec($curl);

投稿が機能していません。param1 に HTMl が含まれていることが原因だと思います。でも使ったらhtmlentities()ダメ。使用してみましurlencode()たが、まだうまくいきません。

4

1 に答える 1

0

&これは特別な URL 区切り文字であることを忘れないでください。
あなたの例<body>test1 & test2</body>では間違って解釈されています。
$poststr慎重に urlencode する必要があります。正しい方法は次のとおりです。

$poststr = "param1=".rawurlencode('<html><head></head><body>test1 & test2</body></html>')."&param2=abcd&param3=eeee";

また、param2 と param3 のすべての部分をエンコードする必要があります。
これを行う最も簡単な方法は、配列とhtml_build_query ()を使用することです。

$params = array();
$params['param1'] = '<html><head></head><body>test1 & test2</body></html>';
$params['param2'] = 'abcd';
$params['param3'] = 'eeee';

//or
//$params = array( 'param1' => '<html><head></head><body>test1 & test2</body></html>',
//                 'param2' => 'abcd',
//                 'param3' => 'eeee'
//               );

$poststr = html_build_query($params);

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "http://www.mytest.com");
curl_setopt($curl, CURLOPT_COOKIEFILE, $cookiefile);
curl_setopt($curl, CURLOPT_COOKIEJAR, $cookiefile);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $poststr);
curl_setopt($curl, CURLOPT_ENCODING, "");
$curlData = curl_exec($curl);
于 2013-04-23T06:51:33.037 に答える