1

For security reasons I have to forward a JSON Object from a Jquery Script to a PHP Script. Here is what I have done yet:

<?php
header('Access-Control-Allow-Origin: *');

$input = @file_get_contents('php://input');
$content = json_encode( $input );

$url = $_GET["trakt_url"]."KEY";

$ch = curl_init($url);                                                                 
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
curl_setopt($ch, CURLOPT_POSTFIELDS, $content);                                                                  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
    'Content-Type: application/json')                                                                       
);                                                                                                                   

$result = curl_exec($ch);
echo $result;
?>

If I log the $content I get the JSON Object but the page($url) at where I post to is getting an empty Object. Does someone have an idea what I am doing wrong?

Solved (someone answered correctly but the answer is deleted?!)

curl_setopt($ch, CURLOPT_POSTFIELDS, "json=$content");

1
  • Set the content length. Commented Feb 23, 2013 at 13:32

1 Answer 1

1

Try setting the content-length in the request header:

curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
    'Content-Type: application/json',                                                                                
    'Content-Length: ' . strlen($content))                                                                       
);                                                                                                                   

You will also need to make sure POSTFIELDS is defined as a urlencoded string, since setting it as an array will override the content-type to multipart/form-data. This will work:

curl_setopt($ch, CURLOPT_POSTFIELDS, "json=$content"); 
Sign up to request clarification or add additional context in comments.

1 Comment

@SimonSchubert Also try this approach

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.