Php 将wp_remote_post()转换为cURL

Php 将wp_remote_post()转换为cURL,php,wordpress,curl,Php,Wordpress,Curl,我使用wp\u remote\u post()对LinkedIn进行API调用,得到了这段WordPress代码 $args = array( 'headers' => array('Content-Type' => 'text/xml'), 'body' => "<?xml version='1.0' encoding='UTF-8' standalone='yes'?><company

我使用
wp\u remote\u post()
对LinkedIn进行API调用,得到了这段WordPress代码

    $args = array(
                'headers' => array('Content-Type' => 'text/xml'),
                'body' => "<?xml version='1.0' encoding='UTF-8' standalone='yes'?><company><id>{$nCompanyID}</id></company>"
            ); 
    $access_token = $datapass->access_token_get();                    
    $params = array('oauth2_access_token' => $access_token); 
    $resource = "https://api.linkedin.com/v1/people/~/following/companies?" . http_build_query($params);        
    $response = wp_remote_post( $resource, $args); 
    $code = $response['response']['code'];
    $body = wp_remote_retrieve_body($response);
    $RV = ($code == '201');
    return $RV;  
$args=array(
'headers'=>array('Content-Type'=>'text/xml'),
'正文'=>“{$nCompanyID}”
); 
$access\u token=$datapass->access\u token\u get();
$params=array('oauth2_access_token'=>$access_token);
$resource=”https://api.linkedin.com/v1/people/~/以下/公司?”。http_build_查询($params);
$response=wp\u remote\u post($resource,$args);
$code=$response['response']['code'];
$body=wp\u remote\u retrieve\u body($response);
$RV=($code=='201');
返回$RV;
它起作用了。现在,我需要将其转换为php cURL。 我尝试了各种在网上找到的php和XML示例,但没有成功。 这是最新的尝试

$access_token = "long_string_of_characters";
$nCompanyID = 2495437;
$xml = "<?xml version='1.0' encoding='UTF-8' standalone='yes'?><company><id>{$nCompanyID}</id></company>";
$url = 'https://api.linkedin.com/v1/people/~/following/companies?oauth2_access_token='.$access_token;

$headers = array(
    "Content-type: text/xml",
    "Content-length: " . strlen($xml)
);

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, array('body' => $xml));
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$data = curl_exec($ch); 
$access\u token=“长字符串\u个字符”;
$nCompanyID=2495437;
$xml=“{$nCompanyID}”;
$url='1https://api.linkedin.com/v1/people/~/following/companys?oauth2_access_token='.$access_token;
$headers=数组(
“内容类型:text/xml”,
“内容长度:”.strlen($xml)
);
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,$URL);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
卷曲设置($ch,卷曲设置桩,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,array('body'=>$xml));
curl_setopt($ch,CURLOPT_HTTPHEADER,$headers);
$data=curl\u exec($ch);
$data
返回一个400错误并显示消息“意外元素:CDATA”。如果我从
CURLOPT_POSTFIELDS
中删除数组并仅使用
$xml
,$data作为空字符串返回


任何帮助都将不胜感激。

如果服务器需要XML POST,那么将数组传递给
CURLOPT_POSTFIELDS
并不会让它满意

请在原始代码中注意构建URL的方法:

$params = array('oauth2_access_token' => $access_token); 
$resource = "https://api.linkedin.com/v1/people/~/following/companies?" . http_build_query($params);        
你会想留着的
http\u build\u query()
如果访问令牌包含特殊字符,则执行所需的转义

考虑到原始代码似乎是使用
$datapass->access\u token\u get()动态生成的,我会怀疑您的访问令牌是否有效

否则看起来一切都是一样的

<?php
$access_token = "long_string_of_characters";
$nCompanyID   = 2495437;
$xml          = "<?xml version='1.0' encoding='UTF-8' standalone='yes'?><company><id>$nCompanyID</id></company>";
$params       = ["oauth2_access_token" => $access_token]; 
$url          = "https://api.linkedin.com/v1/people/~/following/companies?";
$url         .= http_build_query($params);

$headers = [
    "Content-type: text/xml",
    "Content-length: " . strlen($xml)
];

$ch = curl_init(); 
curl_setopt_array($ch, [
    CURLOPT_URL            => $url,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POST           => true,
    CURLOPT_POSTFIELDS     => $xml,
    CURLOPT_HTTPHEADER     => $headers,
]);
$data = curl_exec($ch); 
$access_token];
$url=”https://api.linkedin.com/v1/people/~/下列公司;
$url.=http_build_query($params);
$headers=[
“内容类型:text/xml”,
“内容长度:”.strlen($xml)
];
$ch=curl_init();
curl_setopt_数组($ch[
CURLOPT_URL=>$URL,
CURLOPT_RETURNTRANSFER=>true,
CURLOPT_POST=>true,
CURLOPT_POSTFIELDS=>$xml,
CURLOPT_HTTPHEADER=>$headers,
]);
$data=curl\u exec($ch);

@miken32是的,你的回答非常有用。谢谢很抱歉,在标记为接受时耽搁了很长时间。