dougong2005 2013-12-17 15:00
浏览 631
已采纳

使cURL以json的格式返回

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?

  • 写回答

2条回答 默认 最新

  • doujiongqin0687 2013-12-20 16:34
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题