Reputation: 283
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?
Upvotes: 1
Views: 7121
Reputation: 283
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
Upvotes: 3
Reputation: 1776
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
Upvotes: 2