Curl:Shell脚本的替代Php代码

时间:2015-08-11 10:25:16

标签: php shell curl

我正在尝试将shell代码转换为php。当我们通过shell运行它时,以下代码运行良好,但我想在php中编写代码。

URL="example.com"
curl -k \ -w '\nHTTP STATUS: %{http_code}\n'\

-F "property_id[]=92cf91e5-9032-5e03-8d78-ff1e3af7d9d0" \
-F "property_id[]=6e6b2fbe-75c3-5896-aab0-6d809d6d4ac1" \
-F "property_id[]=c96d6baf-5d6c-564f-9cc4-7ed446e7785c" \
-X 'POST' "$URL" 

我尝试了以下操作,但我得到了#34;未知错误" ......

$params = "--ABC1234\r\n"
. "Content-Type: application/x-www-form-urlencoded\r\n"
. "--ABC1234\r\n"
. "Content-Type: application/x-www-form-urlencoded\r\n"
. "Content-Disposition: form-data; name=\"property_id[]\" 8219cd38-b5c9-4dc6-a119-c983cf1d844b \r\n"
. "\r\n"
. "--ABC1234--";
$request_headers[] = 'Content-Length: ' . strlen($params);
$request_headers[] = 'Content-Type: application/x-www-form-urlencoded;     boundary='. $multipart_boundary;
$ch = curl_init($posturl);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);
$reply = curl_exec($ch);
echo "<pre>";
print_r($reply);

提前致谢

1 个答案:

答案 0 :(得分:1)

终于找到了解决方案。这很简单。

$posturl="https://availability.getaroom.com/api/1.1/room_availability?transaction_id=***&check_in=08/18/2015&check_out=08/19/2015&rooms=1&adults=2&cancellation_rules=1&api_key=**************&auth_token=*********************&currency=AUD";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$posturl);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,"property_id=1234567890");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$getaroomsearch = new SimpleXMLElement(curl_exec($ch));
curl_close ($ch);