Support » Developing with WordPress » cURL vs wp_remote_X

  • Resolved Jan B-Punkt

    (@janbpunkt)


    Heya,

    right now, I’m trying to develop my first plugin.
    Because WordPress comes with its own HTTP-API, I want to switch from cURL (script was used as stand alone) to wp_remote_X, but I’m stuck and need your help.

    While

    $ch = curl_init();
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: ' . $apikey, 'Accept: application/vnd.api+json'));
        curl_setopt($ch, CURLOPT_URL,$url);
        $result=curl_exec($ch);
        curl_close($ch);

    is working fine, I cannot “translate” it to the new wp_remote-thing:

    $result = wp_remote_retrieve_body(wp_remote_get($url, array('sslverify' => false, 'headers' => array('Authorization: '. $apikey,'Accept: application/vnd.api+json'))));

    Result should be some JSON data, but I always get a 451 – Unsupported Media Type

    Hope you guys here can help me.
    Thanks a lot!

    • This topic was modified 4 years, 3 months ago by Jan B-Punkt.
    • This topic was modified 4 years, 3 months ago by Jan B-Punkt.
Viewing 3 replies - 1 through 3 (of 3 total)
  • Hey Jan,

    Hope you are doing well.

    Your code looks good.
    I just run a quick test on my end against a public json api and it workedd just fine.

    
    $url = 'https://jsonplaceholder.typicode.com/todos/1';
    $apikey = 'foo';
    $response = wp_remote_get($url,
                    array(
                        'sslverify' => false,
                        'headers' => array('Authorization: '. $apikey,'Accept: application/vnd.api+json')
                    )
                );
    $result = wp_remote_retrieve_body( $response );
    
    var_dump($result);
    die();

    Of course, this example is not actually using the auth header, but that doesn’t seems to be related to the issue you are experiencing.
    Maybe you are passing some extra parameters to the request payload? That could make the request to force a different content-type header.

    Hope this helps. Take care and let me know if I can be of any help.
    Cheers!

    Thread Starter Jan B-Punkt

    (@janbpunkt)

    Hey sjaure,

    thanks for your reply.

    Maybe you are passing some extra parameters to the request payload? That could make the request to force a different content-type header.

    Well, I have no idea where this could be happening :-/

    I’m only using the code which is shown above.

    Anyway, I think I’ll get figured that out sooner or later.

    Thanks again 🙂

    I’ll leave this open. Maybe someone else might have an idea.

    Thread Starter Jan B-Punkt

    (@janbpunkt)

    Solved.

    Guess, I build the headers-array in a wrong way. The following code is working.
    Thanks 🙂

        $params = array(
    		'sslverify' => false, 
                    'headers' => array(
                          'Authorization' => $apikey,
    		      'Accept' => 'application/json'
                    )
    	);
    	$response = wp_remote_get($url, $params);
    	$result = wp_remote_retrieve_body($response);
        return $result;
    • This reply was modified 4 years, 3 months ago by Jan B-Punkt.
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘cURL vs wp_remote_X’ is closed to new replies.