user2410995
user2410995

Reputation:

Convert curl CLI command to PHP

I have the following curl command and want to use with php curl. The command using curl at the CLI works fine.
Command gathers data from a REST webservice API. The service takes the URL-encoded query string as "q" parameter and returns a simple JSON-encoded result list.

Curl command:

curl -XGET 'user:[email protected]/cdtc-test/service.php/find/ca_objects?q=*' -      d '{
"bundles" : {
"access" : { "convertCodesToDisplayText" : true },
"status" : { "convertCodesToDisplayText" : true },
"ca_entities.entity_id" : {"returnAsArray" : true }
}
}'

I got it working in PHP until the -d flag with the following code:

$curl = curl_init("user:[email protected]/cdtc-test/service.php/find/ca_objects?q=*"); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec ($curl);
curl_close ($curl);

My question is, how do I pass the data following the -d flag using CURLOPT_POSTFIELDS?. I tried with the following code with no resutls:

curl_setopt ($curl, CURLOPT_POST, true);
url_setopt ($curl, CURLOPT_POSTFIELDS, '{
"bundles" : {
"access" : { "convertCodesToDisplayText" : true },
"status" : { "convertCodesToDisplayText" : true },
"ca_entities.entity_id" : {"returnAsArray" : true }
}
}');   

The following is the result of printing curl_getinfo( $curl );

Array ( [url] => user:[email protected]/cdtc-test/service.php/find/ca_objects?q=* [content_type] => [http_code] => 0 [header_size] => 0 [request_size] => 0 [filetime] => 0 [ssl_verify_result] => 0 [redirect_count] => 0 [total_time] => 0 [namelookup_time] => 0 [connect_time] => 0 [pretransfer_time] => 0 [size_upload] => 0 [size_download] => 0 [speed_download] => 0 [speed_upload] => 0 [download_content_length] => -1 [upload_content_length] => -1 [starttransfer_time] => 0 [redirect_time] => 0 [certinfo] => Array ( ) [redirect_url] => ) 

I went trough the documentation and tried everything I could think of but I'm really lost here. Any help will be greatly appreciated.

Upvotes: 2

Views: 385

Answers (2)

troelskn
troelskn

Reputation: 117467

You're making a GET request, not a POST, so I think the php equivalent would be:

$curl = curl_init("user:[email protected]/cdtc-test/service.php/find/ca_objects?q=*&" . urlencode('{
"bundles" : {
"access" : { "convertCodesToDisplayText" : true },
"status" : { "convertCodesToDisplayText" : true },
"ca_entities.entity_id" : {"returnAsArray" : true }
}
}')); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec ($curl);
curl_close ($curl);

Upvotes: 2

user2410995
user2410995

Reputation:

The answer was bulding an array with the data after the -d flag, encode it as JSON and send it. I dind't find it, a developer helped me out. Here's the code:

 $va_body=array(
    "bundles" => array(
        "ca_objects.access" => array("convertCodesToDisplayText" => true),
        "ca_objects.description" => array("convertCodesToDisplayText" => true),
    )
);

$curl = curl_init("user:[email protected]/cdtc-test/service.php/find/ca_objects?q=*");
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, GET); //Not sure why get here...
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($va_body));
$result = curl_exec ($curl);
curl_close ($curl);  

Upvotes: 1

Related Questions