Ram
Ram

Reputation: 45

cURL PHP post not going through but ReturnTransfer is working

I have a newsletter plugin which requires I send form email data to myurl.com/?na=s

The name of the email field needs to be 'ne'

I'm trying to use cURL to submit subscriber data to the newsletter plugin. This is the code I'm using now:

$address = $_POST['emailer'];

$curlvars = [
    'na' => 's',
    'ne' => $address
];

$ch = curl_init('http://www.myurl.com/');
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_USERAGENT, 
  "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $curlvars);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$result = curl_exec($ch);
$curl_error = curl_error($ch);
curl_exec($ch);

curl_close($ch);

This code is all within a php file called send.php which I send the data to through ajax. The send.php file emails the subscriber an email and also submits the subscriber's email to the newsletter plugin.

I added $result to that email so I can see the CURLOPT_RETURNTRANSFER output.

When I enter my own email (for testing) into the form field and click submit, I get the email and at the bottom I get a "subscribe" button (the output of the RETURNTRANSFER).

The subscribe button's data in Inspect Element is:

<form method="post" action="http://www.myURL.com/" id="m_-3594728259508176106form" target="_blank" onsubmit="try {return window.confirm(&quot;You are submitting information to an external page.\nAre you sure?&quot;);} catch (e) {return false;}">
    <input type="hidden" name="q" value="/10442-2/"><input type="hidden" name="na" value="s"><input type="hidden" name="ne" value="[email protected]">
    <input type="hidden" name="ts" value="1475753672">
    <input type="submit" value="Subscribe">
</form>

If I click it, it will submit the email successfully to my newsletter plugin.

Basically, cURL ReturnTransfer output is showing everything correctly but it's not actually POSTing the data to my site when I submit from the site. Only when I hit the generated submit button in the email I receive.

What am I doing wrong?

Thanks in advance!

Upvotes: 0

Views: 176

Answers (1)

anuraj
anuraj

Reputation: 157

i think the problem in your curl_setopt($ch, CURLOPT_POSTFIELDS) function..

you need to change the postfield like this ..

 @curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($curlvars ));
 @curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));

try this code .. in server side dont forgot to json_decode() the coming data

Upvotes: 1

Related Questions