这是我在php中转换的代码。代码链接
string Data = "{" +
" \"request\": { " +
" \"header\": { " +
" \"username\": \"YourUserName\"," +
" \"password\": \"YourPassword\" " +
"}," +
" \"body\": {" +
" \"diamond_id\": \"12345678\" " +
"}" +
"}" +
"}";
string URL = "https://technet.rapaport.com/HTTP/JSON/RetailFeed/GetSingleDiamond.aspx ";
WebRequest webRequest = WebRequest.Create(URL);
webRequest.Method = "POST";
webRequest.ContentType = "application/x-www-form-urlencoded";
Stream reqStream = webRequest.GetRequestStream();
string postData = Data;
byte[] postArray = Encoding.ASCII.GetBytes(postData);
reqStream.Write(postArray, 0, postArray.Length);
reqStream.Close();
StreamReader sr = new StreamReader(webRequest.GetResponse().GetResponseStream());
string Result = sr.ReadToEnd();
我已经在 php 中尝试过,就像下面一样。但我没有得到任何预期的结果。
$jsonData = array(
"request" => array(
"header" => array(
"username"=>"YourUserName",
"password"=>"YourPassword"
),
"body" => array(
"diamond_id" => "12345678"
),
),
);
$url = "https://technet.rapaport.com/HTTP/JSON/RetailFeed/GetSingleDiamond.aspx ";
//Initiate cURL.
$ch = curl_init($url);
//Encode the array into JSON.
$jsonDataEncoded = json_encode($jsonData);
$var = $jsonDataEncoded;
for($i = 0; $i < mb_strlen($var, 'ASCII'); $i++){
ord($var[$i]);
}
//Tell cURL that we want to send a POST request.
curl_setopt($ch, CURLOPT_POST, 1);
//Attach our encoded JSON string to the POST fields.
curl_setopt($ch, CURLOPT_POSTFIELDS, $var);
//Set the content type to application/json
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
//Execute the request
$resultt = curl_exec($ch);
我已经解决了这个问题。我在 php 中转换了这个等效的代码,如下所示
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://technet.rapaport.com/HTTP/JSON/RetailFeed/GetSingleDiamond.aspx",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS =>"{\n\"request\": {\n\"header\": {\n\"username\": \"YourUserName\", \n\"password\": \"YourPassword\"\n\n}, \n\"body\": {\n\t\"diamond_id\": \"12345678\" \n}\n}\n}",
CURLOPT_HTTPHEADER => array(
'Content-Type' => 'application/x-www-form-urlencoded',
'Content-Type' => 'text/plain',
'Cookie' => 'ASP.NET_SessionId=wvxova2bwht020vw4begtpiw'
),
));
$response = curl_exec($curl);
echo $response;
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句