I am trying to submit form from one site to another website by using cURL. However it is giving me some $fields_string variable error (Undefined variable).
This is the very first time I am working with cURL and don’t have much knowledge regarding it. So please guide me to solve this issue where I can submit form successfully.
Information: I am now working on localhost and trying to submit form on localhost another website for testing.
Here is my code
<?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);
?>
This is my html form
<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>
Thanks you
EDIT: NEW CODE
<?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);
?>
If everything is right than may be I am assigning wrong url. I am using question2answer.org script and want to make form where my user can submit form/question from my other website.
EDIT:
@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) "" }
You’re appending to
$fields_string;without setting
$fields_stringto a start value first. Just set it to the empty string first and the error will disappear;