php中的这段代码应该是什么

AA诺曼

这是我在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);
AA诺曼

我已经解决了这个问题。我在 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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在伪c代码片段中避免重复代码的方法应该是什么?

以下迷宫的适当伪代码应该是什么?

Angular9中代码的执行顺序应该是什么?

GenMapFactory中的(elems:(A,B)*)应该是什么类型

Apollo Link 状态中的 __typename 应该是什么?

-1%5在python中应该是什么结果

残留图seaborn中的参数应该是什么

AWS Cloudformation json模板中的“参数”内应该是什么,以及“资源”内应该是什么?

SQL查询应该是什么?

dummy()函数-应该是什么?

以下应该包含整数溢出的代码的正确答案应该是什么?

如果 DB 中不存在针对特定列的值,那么状态代码应该是什么?

在php中设置base_url()。在任何php框架中,base_url()应该是什么?

如果资源不可用于请求的操作,HTTP状态代码应该是什么?

凭证过期错误/异常的HTTP状态代码应该是什么?

如果捕获到信号,程序的退出代码应该是什么?

降级的健康检查的HTTP状态代码应该是什么?

IE 8+浏览器的CSS代码应该是什么

以下代码片段的适当 nunit 测试用例应该是什么?

对于角色是加载服务器缓存的端点,HTTP 响应代码应该是什么?

php读取1Gb文件的方便内存限制应该是什么?

.cpp文件中纯虚函数的正确返回值应该是什么?

next-firebase-auth 中的 COOKIE_SECRET_CURRENT 应该是什么?

多标签分类中的最后一层应该是什么?

Spark Streaming 中 setJars() 方法的输入应该是什么

Micronaut 中反应式控制器方法的返回类型应该是什么?

angularJs指令中templateUrl的正确路径应该是什么?

使用cordova应用程序,openFB中的oauthRedirectURL应该是什么样的?

控制文件中的“ misc-depends:”应该是什么?