Это код, который я конвертирую в 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);