使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?

drgweamoi473182981
drgweamoi473182981 在这种情况下,文档是你的朋友。请参阅下面的@Andrew答案。
接近 7 年之前 回复
doumao6048
doumao6048 不,我没有外部服务器的控制权。这是flickr。
接近 7 年之前 回复
du521521521
du521521521 您需要将xml解析为所需的数组,然后对其进行json_encode。如果回复不在您的手中或来自其他公司。如果有一个参数要传递给json响应,可能会问你发送请求的人
接近 7 年之前 回复
duansao20000508
duansao20000508 您需要服务器返回json,或者您需要将xml转换为json。请参阅此stackoverflow.com/questions/8830599/php-convert-xml-to-json
接近 7 年之前 回复
douxunchen3498
douxunchen3498 您是否可以控制外部服务器返回的内容(即,它是您的服务器?)。如果它返回xml则设置为返回xml。
接近 7 年之前 回复

2个回答



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

但是,您可以使用以下方法轻松地将此xml片段转换为json:</ p>

  $ xml_snippet = simplexml_load_string($ result  ); 
$ json_convert = json_encode($ xml_snippet);
$ json = json_decode($ json_convert);
</ code> </ pre>

现在任何需要使用<的调用 代码> cURL的</ code>返回的数据只使用 $ json </ code>变量。</ p>

特别感谢@AntonioMax over:PHP将XML转换为JSON </ p>
</ div>

展开原文

原文

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



我假设您使用flickr API。 使用 http://www.flickr上的官方文档中所述的format = json参数。 com / services / api / response.json.html </ p>
</ div>

展开原文

原文

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

douchilian1009
douchilian1009 一旦我将HTTPHEADER array()添加到cURL方法,我收到(在xml中)错误代码:100 msg =“无效的API密钥(密钥格式无效)”???
接近 7 年之前 回复
dongzouqie4220
dongzouqie4220 是的,我正在使用flickr的api。 我也在使用他们的格式参数......这就是为什么我很困惑。 format = $ this-> format等于json。 不确定为什么它在那个参数时返回xml。 设定好了!
接近 7 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐