0

I am able to send the CURL request to a test http API and getting the response as well.

My CURL request is like:

$request = "";
$param['auth-userid'] = '449735';
$param['api-key'] = 'apikey';
$param['domain-name'] ='sambalpurodisha';
$param['tlds']='com';
foreach ($param as $key => $val) {$request.= $key ."=".urlencode($val);    
$request .="&";}
$request=substr($request,0,strlen($request)- 1 );
$url = "https://test.httpapi.com/api/domains/available.json?".$request;

$ch = curl_init($url);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
$result = curl_exec( $ch );
curl_close( $ch );

I am getting a response like:
{"sambalpurodisha.com":{"status":"available","classkey":"domcno"}}

Now I have tried everything to format the result in more readable manner like Domain - sambalpurodisha.com is available for purchase.

I have tried number of suggestions here at stack, but none worked for me. A direction where I can look for

1 Answer 1

1

You'll need to decode the returned JSON string as either a JSON object or an associative array, then iterate through it:

$rawJSON = '{"sambalpurodisha.com":{"status":"available","classkey":"domcno"}}';

$jsonArray = json_decode($rawJSON, true);
foreach($jsonArray as $key => $val) {

    echo 'Domain: ' . $key . '  status: ' . $val['status'] . "\n";

}
Sign up to request clarification or add additional context in comments.

5 Comments

The problem is I don't know what value will be there in the variables. domain-name can be anything, status->value and classkey -> value will keep changing. so $rawjason = to have have the expected variables.
That should work fine. Just assign the response you get back from cURL to rawJSON. The hardcoded string here is simply intended to be an example.
Ok I have tried adding $rawJSON=$result; and then $jsonAraay =...........................} But I am getting the same result i.e. '{"sambalpurodisha.com":{"status":"available","classkey":"domcno"}}'; and not the expected result. Do I need to do anything different.
Maybe I'm misunderstanding your question. I can't give you a full code sample, since the URL in your question above (test.httpapi.com) doesn't appear to be accepting requests. When you try the parsing code I supplied, what exactly is getting echoed to the console?
Right I think I have messed up my code hence not getting the result. Now it's working fine as expected.

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.