Jump to content

converting curl command line to libcurl


refr3sh

Recommended Posts

hello guys!

 

i'm trying to convert this curl command line to php libcurl

curl -X POST https://test.stellar.org:9002 -d '
{
  "method": "account_currencies",
  "params": [
    {
      "account": "gM4Fpv2QuHY4knJsQyYGKEHFGw3eMBwc1U"
    }
  ]
}'

What I've done so far:

 

<?php
	$method = "account_currencies";
	$params = Array("account" => "gM4Fpv2QuHY4knJsQyYGKEHFGw3eMBwc1U");
	$request = xmlrpc_encode_request($method, $params);
		
        $ch = curl_init(); 
		
	$headers[] = "Content-Type: text/xml" ;
	$headers[] = "Content-length: ".strlen($request) . "\r\n";
	$headers[] = $request;
	
        curl_setopt($ch, CURLOPT_URL, "https://test.stellar.org"); 
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_PORT , 9002);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
	curl_setopt($ch, CURLOPT_HTTPHEADER, $headers );

	$response = curl_exec($ch);
		
        curl_close($ch);
		
	echo $response;
?>

It should give me back a xml response but I just get: "Unable to parse request"

 

Can anyone tell me whats wrong with the code?

 

Link to comment
Share on other sites

I'm not sure why you're trying to use XML, as the API docs indicate you should be using JSON. Something like this:

<?php
    $request = array(
        'method' => "account_currencies",
        'params' => array('account' => "gM4Fpv2QuHY4knJsQyYGKEHFGw3eMBwc1U")
    );
    $ch = curl_init();

    $headers[] = "Content-Type: application/json";
    $headers[] = "Content-length: ".strlen($request) . "\r\n";
 
    curl_setopt($ch, CURLOPT_URL, "https://test.stellar.org");

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    curl_setopt($ch, CURLOPT_PORT , 9002);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($request));
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers );

 
    $response = curl_exec($ch);

    curl_close($ch);



    echo $response;
?>
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.