1

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?

8
  • count will send only num of array not the data Commented Apr 17, 2015 at 8:45
  • Hi Richie, thanks for answer, you were right. But even if I remove count i get answer NULL Commented Apr 17, 2015 at 8:46
  • try with this curl_setopt($ch,CURLOPT_POSTFIELDS, $yourArray) also check for errors Commented Apr 17, 2015 at 8:49
  • Unfortunately same answer as before, NULL. I am thinking if I dont need to send it like JSON format or something. Because API example is in JSON Commented Apr 17, 2015 at 8:51
  • If the webservice expecting a JSON string you should provide one, try using json_encode($fields) instead of just $fields Commented Apr 17, 2015 at 8:54

3 Answers 3

2

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);
Sign up to request clarification or add additional context in comments.

3 Comments

I really apreaciate your help Richie, but looks my skill is not good enugh, I'm not sure how to create json with some type above manufacturer, submodel, model. Is there a chance you can write an example?
that's a different question you should ask a different. If you can create a array like that then json_encode will work
Catchable fatal error: Object of class stdClass could not be converted to string in $data_json. Full COde: pastebin.com/quADrYxe
0

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)

Comments

0

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

1 Comment

I strongly recommend you use json_encode rather than try to encode the JSON yourself, or you'll quickly get caught by issues with quotes and the like.

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.