我在php中有这个函数,但在wordpress中HttpRequest
方法不起作用,我不知道如何使用wordpress函数传输信息。我的php函数:
$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;
}
我在WP桅杆使用中读到过
wp_remote_post, wp_remote_get
我试着这样做:
$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 );
但我不知道我的数据格式是否正确。
最合适的回答,由SO网友:Lutty 整理而成
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);