cURLを使用して、あるサイトから別のWebサイトにフォームを送信しようとしています。ただし、$ fields_string変数エラー(未定義の変数)が発生します。
私がcURLを使用するのはこれが初めてであり、それに関する知識はあまりありません。それで、私がフォームを首尾よく提出することができるこの問題を解決するために私を導いてください。
情報:現在、ローカルホストで作業しており、テストのためにローカルホストで別のWebサイトにフォームを送信しようとしています。
これが私のコードです
<?php
$url = 'http://localhost/qa/ask';
$fields = array(
'title'=>urlencode($_POST['title']),
'content'=>urlencode($_POST['content']),
'tags'=>urlencode($_POST['tags']),
'q_notify'=>urlencode($_POST['q_notify']),
);
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string,'&');
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
$result = curl_exec($ch);
curl_close($ch);
?>
これは私のhtmlフォームです
<form action="submit.php" method="POST">
<INPUT NAME="title" TYPE="text" CLASS="qa-form-tall-text custom-ask-text" value="" placeholder="Ask your question" autocomplete="off" role="textbox">
<TEXTAREA NAME="content" CLASS="qa-form-tall-text"></TEXTAREA>
<INPUT NAME="tags" TYPE="text" CLASS="qa-form-tall-text custom-ask-text" value="">
<label><input type="checkbox" class="qa-form-tall-checkbox" checked="" value="1" name="q_notify">Email me if my question is answered or commented on</label>
<INPUT CLASS="custom-ask-submit" TYPE="submit" value="ask">
<INPUT TYPE="hidden" NAME="doask1" VALUE="1">
</form>
ありがとうございます
編集:新しいコード
<?php
$url = 'http://localhost/qa/ask';
$fields = array(
'title'=>$_POST['title'],
'content'=>$_POST['content'],
'tags'=>$_POST['tags'],
'q_notify'=>$_POST['q_notify'],
);
$fields_string = '';
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields);
$result = curl_exec($ch);
curl_close($ch);
?>
すべてが正しい場合は、間違ったURLを割り当てています。question2answer.orgスクリプトを使用していて、ユーザーが他のWebサイトからフォーム/質問を送信できるフォームを作成したいと考えています。
編集:@GBD
Curl error: array(22) { ["url"]=> string(23) "http://localhost/qa/ask" ["content_type"]=> string(24) "text/html; charset=utf-8" ["http_code"]=> int(200) ["header_size"]=> int(443) ["request_size"]=> int(211) ["filetime"]=> int(-1) ["ssl_verify_result"]=> int(0) ["redirect_count"]=> int(0) ["total_time"]=> float(0.141) ["namelookup_time"]=> float(0) ["connect_time"]=> float(0.016) ["pretransfer_time"]=> float(0.016) ["size_upload"]=> float(87) ["size_download"]=> float(16130) ["speed_download"]=> float(114397) ["speed_upload"]=> float(617) ["download_content_length"]=> float(-1) ["upload_content_length"]=> float(87) ["starttransfer_time"]=> float(0.141) ["redirect_time"]=> float(0) ["certinfo"]=> array(0) { } ["redirect_url"]=> string(0) "" }