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
https://forums.phpfreaks.com/topic/294910-converting-curl-command-line-to-libcurl/
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;
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.