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?