使 cURL 以 json 格式返回


Making cURL return in the format of json

我目前正在尝试使用 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_encodeing他们用于POSTFIELDS选项的变量,我想知道这是否是它无法正常工作的原因?

您无法更改 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 将 XML 转换为 JSON

我假设你使用flickr API。使用 format=json 参数,如官方文档中所述 http://www.flickr.com/services/api/response.json.html