1

I'm currently trying to use a cURL method to POST data to an external server and need it to return data in json formatted code, right now the only format it returns in is xml... but I can't use that data to continue on.

Below is what I've got so far.

$ch = curl_init();
    curl_setopt( $ch, CURLOPT_URL, $flickr_upload ); 
    curl_setopt( $ch, CURLOPT_POST, true );
    curl_setopt( $ch, CURLOPT_POSTFIELDS, $parameters_string );
    $result = curl_exec( $ch ); 
curl_close( $ch );

I've heard / read some stuff about needing to add an HTTPHEADER option, which I have tried doing in the following manner:

curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));

But when I tried this I just got an error from the external site that I'm POST-ing to.

Below is my entire function...

public function uploadPhotos( $photo, $title = null, $tags = null ) {

    // Function specific variables
    $flickr_upload          = $this->flickr_upload_call;
    $access_token               = "my_access_token";
    $access_token_secret        = "my_access_token_secret";

    // Authorization method
    $url  = "format=" . $this->format;
    $url .= "&nojsoncallback=1";
    $url .= "&oauth_consumer_key=" . $this->flickr_key;
    $url .= "&oauth_nonce=" . $this->nonce;
    $url .= "&oauth_signature_method=" . $this->sig_method;
    $url .= "&oauth_timestamp=" . $this->timestamp;
    $url .= "&oauth_token=" . $access_token;
    $url .= "&oauth_version=1.0";

    $baseurl            = "POST&" . urlencode( $flickr_upload ) . "&" . urlencode( $url );
    $hashkey            = $this->flickr_secret . "&" . $access_token_secret;
    $oauth_signature    = base64_encode( hash_hmac( 'sha1', $baseurl, $hashkey, true ));

    $url_parameters = array(
        'format'                =>$this->format,
        'nojsoncallback'        =>'1',
        'oauth_consumer_key'    =>$this->flickr_key,
        'oauth_nonce'           =>$this->nonce,
        'oauth_signature_method'=>$this->sig_method,
        'oauth_timestamp'       =>$this->timestamp,
        'oauth_token'           =>$access_token,
        'oauth_version'         =>'1.0',
        'oauth_signature'       =>$oauth_signature
    );

    //* Now that we have encoded the parameters for our ouath_signature
    //* and have reformated them for the url we need to send... we must
    //* re-urlencode them too. 

    $parameters_string = "";
    foreach ( $url_parameters as $key=>$value )
        $parameters_string .= "$key=" . urlencode( $value ) . "&";

    $parameters_string = rtrim( $parameters_string, '&' );
    $url = $flickr_upload . "&" . $parameters_string;

    $ch = curl_init();
        curl_setopt( $ch, CURLOPT_URL, $flickr_upload ); 
        curl_setopt( $ch, CURLOPT_POST, true );
        curl_setopt( $ch, CURLOPT_POSTFIELDS, $parameters_string );
        $result = curl_exec( $ch ); 
    curl_close( $ch );

    var_dump( json_decode( $result, true ));

    if (!curl_exec( $ch )) {
        // if curl_exec() returned false and thus failed
        echo 'An error has occurred.';
    }

} // end Upload images

I have seen some people json_encodeing the variable that they use for the POSTFIELDS option and I'm wondering if that is why it's not working correctly?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Designer 17
  • 283
  • 1
  • 4
  • 17
  • 1
    Do you have control over what the external server returns (I.e. is it your server?). If it is returning xml then it is set to return xml. – superphonic Dec 17 '13 at 15:02
  • You either need the server to return json, or you need to convert the xml to json. See this http://stackoverflow.com/questions/8830599/php-convert-xml-to-json – Mattt Dec 17 '13 at 15:03
  • 1
    You will need to parse the xml into a desired array and then json_encode it. If the response is out of your hands or from another company. Maybe ask the people you are sending the request to if there is a parameter to pass to make it a json response – Ryan Dec 17 '13 at 15:04
  • @superphonic: No, I do not have control of the external server. It is flickr. – Designer 17 Dec 17 '13 at 16:04
  • In which case the documentation is your friend. See @Andrew answer below. – superphonic Dec 17 '13 at 16:05

2 Answers2

3

You cannot change the return format of a flickr upload request... it always returns xml.

You can, however, quite easily convert this xml snippet to json using the following method:

$xml_snippet = simplexml_load_string( $result );
$json_convert = json_encode( $xml_snippet );
$json = json_decode( $json_convert );

Now any calls that need to use the cURL's returned data just use the $json variable.

Special thanks to @AntonioMax over at: PHP convert XML to JSON

Community
  • 1
  • 1
Designer 17
  • 283
  • 1
  • 4
  • 17
2

I assume you use flickr API. Use format=json parameter as stated in official docs at http://www.flickr.com/services/api/response.json.html

Andrew
  • 1,756
  • 3
  • 18
  • 31
  • Yes, I am using flickr's api. Also I am using their format parameter already... that's why I'm so confused. `format = $this->format` is equal to `json`. Not sure why it's returning xml when that param. is set! – Designer 17 Dec 17 '13 at 16:09
  • Once I add `HTTPHEADER array()` to the cURL method I receive (in xml) `error code: 100 msg="Invalid API Key (Key has invalid format)"` ??? – Designer 17 Dec 17 '13 at 16:49