0

I have not used Curl much in the past and am getting a bit confused.

I am trying to grab the info in my form and post to the URL set in my CURLOPT_URL.

I can get the basic Json to post in postman using the following

{
  "type":"bio",
  "title":"new test5"
}

but when i try and add the data from my html form it dose not work, I think I have gone a bit wrong could someone correct me and let me know what I havedone wrong, thanks.

Full Code

   <form method = 'POST'>
        <div class="">
            <label><b>Title</b></label>
            <input type="text" placeholder="Enter Title" name="titletext" required>
            <button type="submit">Send</button>
        </div>
    </form>
<?php

$curl = curl_init();

if(isset($_POST['titletext']))
curl_setopt_array($curl, array(
    CURLOPT_URL => "http://localhost/communicator-app/bensapi/node/",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => "",
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 30,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => "POST",
    CURLOPT_POSTFIELDS => array
    (
        'type' => 'bio',
        'title' => $_POST['titletext']
    ),

    CURLOPT_COOKIE => file_get_contents( 'session_cookie.txt' ),
    CURLOPT_HTTPHEADER => array(
        "content-type: application/json"
    ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
    echo "cURL Error #:" . $err;
} else {
    echo "POSTED !";
}
Beep
  • 2,737
  • 7
  • 36
  • 85
  • do you get any error message? how you track that its not working? – BetaDev Apr 19 '17 at 16:24
  • I track its not working when I check the xml api, not getting a error, but ill check the network tab – Beep Apr 19 '17 at 16:25
  • did you try `CURLOPT_POSTFIELDS =>json_ecode(your post data array).......` and this might be helpful http://stackoverflow.com/questions/11079135/how-to-post-json-data-with-php-curl – BetaDev Apr 19 '17 at 16:28
  • 1
    see missing curl params here for json type POST http://stackoverflow.com/questions/21271140/curl-and-php-how-can-i-pass-a-json-through-curl-by-put-post-get – BetaDev Apr 19 '17 at 16:32
  • thanks, +! for intimation but i cant seen what my post is missing – Beep Apr 19 '17 at 16:39

1 Answers1

0

This might work for you because working at my side using your code
index.php with your form

//Your form
<?php 
if(isset($_POST['titletext'])){
    $ch = curl_init();
    $data_json = json_encode(array
    (
        'type' => 'bio',
        'title' => $_POST['titletext']
    ));
    curl_setopt($ch, CURLOPT_URL, "http://localhost/test/second.php");
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS,$data_json);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_COOKIE, "your cookie");

    $response  = curl_exec($ch);
    curl_close($ch);

    print_r($response);
}

second.php

<?php
print_r(file_get_contents("php://input"));
?>

I am submitting your form to this second.php using curl and i get output of print_r($response); as {"type":"bio","title":"dsfgdfsgv"}

BetaDev
  • 4,516
  • 3
  • 21
  • 47