webservice php+json

Asked

Viewed 850 times

1

Hello!

I would like help to integrate with webservice via url, for example:

$list_result = '{"titulo": '. 
  '[{"id":$id_primary}'.
']}';

$json = json_encode($list_result);

//echo $json;

//API Url
$url = 'https://local-que-devo-enviar.com.br/arquivo';



$ch = curl_init($url);

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(

    'Content-Type: application/json',
    'Content-Length: ' . strlen($json))
);


//retorno   
$jsonRet = json_decode(curl_exec($ch)); 

var_dump($jsonRet);
  • I would like help to understand if I am assembling the data array correctly, how do I get a return and if the code stays that way. 'Cause I’m not getting back from the webservice...
  • Are you wearing a json_encode of something that is already "like" a JSON?!

2 answers

1


It is on the way, but deviated at the time of creating the JSON that is sending..

To encode something in JSON with PHP you must pass an array as parameter. Change the first lines for this, test and tell the result in the comments to refine our response:

$list_result = array(
    'titulo' => array(
        array(
            'id' => $id_primary,
        ),
    ),
);

$json = json_encode($list_result);

Ah! And, add this to your request settings to view more server response data:

curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);

And, have to close the Curl at the end and notice that I removed the function json_decode at that time only to check the response of the server that is with header:

$jsonRet = curl_exec($ch); 
curl_close($ch);
var_dump($jsonRet);
  • I realized I need a group and a subgroup in the matrix.. how would that look... "test":9999, "test":99999, "test":"LINDL0001", "items":[ ' "item1DoTeste1":' "item1":"123", "item2":11, "item3":"22/09/2006", }, "item2DoTeste1":ː "a":123, "b":"2 x", "c":3, "d":"s 44", },

  • I managed to assemble with this idea. It was cool, only the return that nothing concrete.. with the var_dump, appears bool(false)

  • I tested again, now I’m getting a consistent feedback.. So, solved!!! Thanks!!! = D

  • If you can show me more details on how to capture the data return, I think I’ll need.. =/

  • and you have already seen an error in the return called: 'businessException' ?

  • 1

    businessException must be an error that the Webservice you are trying to consume is generating. And when data capture depends on the data, the return is JSON, XML, HTML.. instead of using var_dump uses print_r and if there is any specific question, create a new question and do not forget to present the context and inform the code that is working!!

  • Ah! As for the exception, look for API documentation or a contact with the developers to understand what went wrong for this exception to be generated.

  • Perfect... and about the data, I would like for example to capture a name coming from the webservice, which will be based on an id I sent for example. With json/php/html...

  • Ahh, and to send token and information of this kind in the structure header, do you know where the code stands? (based on the structure placed at the beginning)

  • 1

    Here is everything you can configure http://php.net/manual/en/function.curl-setopt.php. And to add header the option is CURLOPT_HTTPHEADER which should receive an array of strings

  • And, about capturing a Omo coming from the webservice.. this is very broad has no way to respond. Ask specific new questions with examples that there will always be someone trying to answer.

Show 6 more comments

0

I realized that I need a group and a subgroup in the matrix. What would that look like...
{
"test":9999, "test": 99999, "test":"LINDL0001", "items":[
{
"item1DoTeste1":{
"item1":"123", "item2": 11, "item3":"22/09/2006",
}, "item2DoTeste1":{
"to":123, "b":"2 x", "c":3, "d":"s 44", },

  • The issue of the bracket in the group and subgroup I got, I’m still testing ;

  • This part of the brackets was solved tbm.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.