我在复制某些在Powershell中正常工作的php时遇到问题。
我的powershell脚本是:
$url = 'https://private.service-now.com/api/now/table/change_request?sysparm_query=u_requesting_group.name=ibgportal&sysparm_display_value=true&sysparm_exclude_reference_link=true'
function generateSNOWHeaders {
#Pull the SNOW password from encrypted string in the SecureStrings.txt file and use it to create the authentication credentials.
$username = "private"
$password = ConvertTo-SecureString -String "private" -AsPlainText -Force
$cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $username, $password
$encodedCreds = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes( $cred.UserName + ":" + $cred.GetNetworkCredential().password ) )
$basicAuthValue = "Basic $encodedCreds"
$returnHeaders = @{ "Authorization" = $basicAuthValue; "Accept-Language" = 'application/json' }
return $returnHeaders
}
$headers = generateSNOWHeaders
$returnedResult = Invoke-RestMethod -Method Get -Uri $url -Headers $headers -Body ( ConvertTo-Json $body ) -ContentType "application/json"
write-host $returnedResult.Result
我的php脚本调用是:
$user = 'private';
$pass = 'private';
$url = "https://private.service-now.com/api/now/table/change_request?sysparm_query=u_requesting_group.name=ibgportal&sysparm_display_value=true&sysparm_exclude_reference_link=true";
$userPass = base64_encode($user.':'.$pass);
$curl = curl_init();
curl_setopt_array($curl, array(
//CURLOPT_CAINFO => dirname(__FILE__)."/cacert.pem",
CURLOPT_URL => "$url",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 300,
//CURLOPT_HTTPAUTH => CURLAUTH_BasicAuth,
//CURLOPT_USERPWD, "$userPass",
CURLOPT_VERBOSE => true,
//CURLOPT_PORT => 80,
//CURLOPT_COOKIEJAR => dirname(__FILE__) . '/cookie.txt',
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"Authorization: Basic $userPass",
"Accept-Language: application/json",
//"Content-Type: application/json",
//"Host: service-now.com",
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
请忽略评论。我正在同一台服务器上运行两个脚本,而php脚本则响应超时错误。为什么Powershell可以工作,但是php却不能工作?