使cURL以json的格式返回

时间:2013-12-17 15:00:59

标签: php json curl

我目前正在尝试使用cURL方法将数据发送到外部服务器,并且需要它以json格式的代码返回数据,现在它返回的唯一格式是xml ...但是我无法使用该数据将继续。

以下是我到目前为止所做的事情。

$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 );

我听说/读过一些关于需要添加HTTPHEADER选项的内容,我尝试过以下方式:

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

但是当我尝试这个时,我刚收到来自我正在发布的外部网站的错误。

以下是我的整个功能...

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

我见过一些人json_encode他们用于POSTFIELDS选项的变量,我想知道这是不是为什么它不能正常工作?

2 个答案:

答案 0 :(得分:3)

您无法更改flickr上传请求的返回格式...它始终返回xml。

但是,您可以使用以下方法轻松地将此xml代码段转换为json:

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

现在,任何需要使用cURL's返回数据的调用都只使用$json变量。

特别感谢@AntonioMax:PHP convert XML to JSON

答案 1 :(得分:2)

我假设您使用flickr API。使用http://www.flickr.com/services/api/response.json.html

官方文档中所述的format = json参数