0

I have the function in php but in wordpress the HttpRequest method does not work and I did not understand how to transmit the information with the wordpress functions. My php function:

$request = new HttpRequest();
$request->setUrl('https:WEBSITE');
$request->setMethod(HTTP_METH_POST);
$request->setQueryData(array(
  'api_token' => 'APIKEY'
));
$request->setHeaders(array(
  'cache-control' => 'no-cache',
  'content-type' => 'application/json'
));
$request->setBody('JSON');
try {
  $response = $request->send();

  echo $response->getBody();
} catch (HttpException $ex) {
  echo $ex;
}

I have read that in WP mast use wp_remote_post, wp_remote_get and i try this:

$url = 'URL';
$args = array(
    'headers' => array( 'cache-control' => 'no-cache', 'content-type' => 'application/json', 'api_token' => 'APIKEY' ),
    'body' => '{"pass":{"serial_number":"123456","Name":"Michele","Date":"2017/11/11","Type":"Abb.Annuale","Issued":"Genn.2016","Al":"Dic.2018","Number":"0123456789412"}}',
);
$response = wp_remote_post( $url, $args );
$body = wp_remote_retrieve_body( $response );

But don't understand if my data is fine format.

  • HttpRequest isn't a standard PHP class or WordPress class. I can't tell you if your WordPress version is correct without knowing what you're trying to do. – Jacob Peattie Dec 1 '17 at 10:58
  • I try to send a json code to a server and have an answer from the same I thought that my question was clear – Lutty Dec 1 '17 at 11:19
  • Well your question didn't include the data you were trying to send, so I wasn't clear on what you were trying to do with array('JSON'). Have you tried this and it's not working? – Jacob Peattie Dec 1 '17 at 11:29
  • I have update my attempt but think have wrong APIKEY location – Lutty Dec 1 '17 at 11:37
  • Well that would depend on the API – Jacob Peattie Dec 1 '17 at 11:44
0

This my solution:

$url = '##MYURL##?api_token=##APIKEY##';
$args = array(
    'headers' => array( '','cache-control' => 'no-cache', 'content-type' => 'application/json' ),
    'body' => '{"pass":{"serial_number":"123456","Name":"Michele","Date":"2017/11/11","Type":"Abb.Annuale","Issued":"Genn.2016","Al":"Dic.2018","Number":"0123456789412"}}',
);
$response = wp_remote_post( $url, $args );

$body = wp_remote_retrieve_body( $response );
var_dump($body);
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.