Andurit
Andurit

Reputation: 5762

CURL send POST data

I'm using CURL to get response from some webservice. Problem is that it need some POST data in format like this:

{
    "type": {
        "manufacturer": "AX",
        "model": "AX",
        "submodel": "AX"
    }
}

My script looks like:

$url = 'URL';
//  Initiate curl
$ch = curl_init();
// Disable SSL verification
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Will return the response, if false it print the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Set the url
curl_setopt($ch, CURLOPT_URL,$url);
// Execute
$result=curl_exec($ch);
// Closing
curl_close($ch);

// Will dump a beauty json :3
var_dump(json_decode($result, true));

I found a way how to use POST in CURL and it should looks like:

$fields = array(
                'manufacturer' => urlencode('AX'),
                'model' => urlencode('AY'),
                'submodel' => urlencode('AZ')
                );
 curl_setopt($ch,CURLOPT_POST, $fields);

But this didn't work for me at all :(, always getting answer NULL. Please can somebody help me to figurate out what I'm doing wrong?

Upvotes: 1

Views: 9954

Answers (3)

Andurit
Andurit

Reputation: 5762

Thank you all for help, you lead me right way to find solution on this problem.

Problem was in way i Insert json at start which you helped me with, after some googling I found out I miss header Accept: application/json as well,

So Final version looks like:

$json = '{
    "type": {
        "manufacturer": "AX",
        "model": "AY",
        "submodel": "AZ"
    }
}';
$ch = curl_init();
$url = 'WEB';
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Accept: application/json'));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$json);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response  = curl_exec($ch);

if($response === false){
    echo 'Curl error: ' . curl_error($ch);
}
else{
    print_r($response);
}
curl_close($ch);

Thanks all and have a nice weekend

Upvotes: 0

Ravi Kant Mishra
Ravi Kant Mishra

Reputation: 768

Create a sample json like them and try with this. you can get the sample data from postman. As you said you are able to post via postman.

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
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);
$response  = curl_exec($ch);

if($response === false){
    echo 'Curl error: ' . curl_error($ch);
}
else{
    print_r($response);
}
curl_close($ch);

Upvotes: 2

detheridge02
detheridge02

Reputation: 650

Are you actually posting data via a POST request or requesting data via a GET request? If the latter have a look at Guzzle as it allows you to set a JSON body whereas PHP cURL currently won't. Using Guzzle it's as simple as :

$request = $guzzleClient->createRequest('GET', $endpoint);
$request->setBody(Stream::factory($jsonQuery));
$response = $guzzleClient->send($oRequest)

Upvotes: 0

Related Questions