I am having some trouble with using Proxy route with file_get_contents. When i use it with cURL it works, but not with this code:
$headers = array('Content-Type: text/xml');
$url = "http://google.com/";
$proxy = 'http://xx.xxx.xxx.xxx:443';
$opts = [
"http" => [
'proxy' => $proxy,
"method" => "POST",
"header" => implode("\r\n", $headers),
'timeout' => 500,
"content" => $request,
],
];
$stream_context = stream_context_create($opts);
$data = file_get_contents($url, false, $stream_context);
return $data;
Problem is that this does not hit my proxy server first, but goes direct to google.com.
when i use this code with cURL it works perfect:
$handle = curl_init();
$url = "http://google.com";
$proxy = 'xx.xxx.xxx.xxx:443';
// Set the url
curl_setopt($handle, CURLOPT_URL, $url);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_PROXY, $proxy);
$output = curl_exec($handle);
curl_close($handle);
echo $output;
Could anyone see what the problem is?
I think you are using the wrong Protocol
try using:
$proxy = 'tcp://xx.xxx.xxx.xxx:443';
also try adding the following to your $opts array:
'request_fulluri'=>true
Related
I am trying to use Thycotic PAM API. According to their documentation, The following is a sample HTTP POST request. The placeholders shown need to be replaced with actual values.
POST /SecretServer/webservices/SSWebservice.asmx/GetUser HTTP/1.1
Host: 192.168.3.242
Content-Type: application/x-www-form-urlencoded
Content-Length: length
token=string&userId=string
I can get token string and user ID from the app. With this data, following is the PHP code I am trying
$url = 'https://192.168.3.242/SecretServer/webservices/SSWebservice.asmx/GetUser';
$data = array(
'token' => 'token_string',
'userId' => 8
);
// use key 'http' even if you send the request to https://...
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data)
)
);
$context = stream_context_create($options);
$result = json_decode(file_get_contents($url, false, $context));
if ($result === FALSE) { /* Handle error */ }
var_dump($result);
I also tried this way:
function curl_get_contents($url)
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$url = 'https://192.168.3.242/SecretServer/webservices/SSWebservice.asmx/GetUser?token=token_string&userId=8 HTTP/1.1';
$json = json_decode(curl_get_contents($url));
var_dump($json);
Both of them are returning nothing. Any suggestion is much appreciated.
curl_setopt($ch ,CURLOPT_POST, 1);
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch ,CURLOPT_POSTFIELDS, "token=string&userId=string");
you must use this parameters
You know how in PHP there's a method called file_get_content that gets the content of the page for the provided url? Is there an opposite method for it? Like, for example, file_post_content, where you can post data to external websites? Just asking for educational purposes.
You can use without cURL but file_get_contents PHP this example:
$url = 'URL';
$data = array('field1' => 'value', 'field2' => 'value');
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data),
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
var_dump($result);
See the PHP website: http://php.net/manual/en/function.file-get-contents.php#102575
Could write one:
<?php
function file_post_content($url, $data = array()){
// Collect URL. Optional Array of DATA ['name' => 'value']
// Return response from server or FALSE
if(empty($url)){
return false;
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, 1);
if(count($data)){
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
}
// receive server response ...
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$svr_out = curl_exec ($ch);
curl_close ($ch);
return $svr_out;
}
?>
i want to receive errors from the sms host server
curl_error curl_errno curl_strerror all these function returns error but not in the way i want , mysmshost say that you will receive code like, 202 for wrong mobile number, 301 for wrong authentication, i want to receive those error code please help.
this is my code.
//sending message
$authKey = "my_authentication_key";
$senderId = "sender";
$message = urlencode("Hello Its Working..");
$route = "1";
$campaign = 'Default';
//Prepare you post parameters
$postData = array(
'authkey' => $authKey,
'mobiles' => $number,
'message' => $message,
'sender' => $senderId,
'route' => $route,
'campaign' => $campaign
);
//API URL
$url="http://sms.mysmshost.com/sendhttp.php";
// init the resource
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $postData
//,CURLOPT_FOLLOWLOCATION => true
));
//Ignore SSL certificate verification
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
//get response
$output = curl_exec($ch);
//Print error if any
if(curl_errno($ch))
{
echo curl_error($ch)."-curl_error<br/>";
$chcode =curl_errno($ch);
echo $chcode;
echo '<br/>error:' . curl_strerror($chcode)."<br/>";
}
curl_close($ch);
echo $output;
Curl_errorno() is not executing, even if i print them without if condition they give no result.
Try this
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_ENCODING, '');
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postData));
$server_output = curl_exec($ch);
curl_close($ch);
print_r($server_output);
Can someone help to convert this PHP Curl to UrlFetch ? This is used for Apple iTunes verifyReceipt
if (getiTunesProductionLevel($app_id)=="sandbox" || $sandbox_override == TRUE) {
$endpoint = 'https://sandbox.itunes.apple.com/verifyReceipt';
}
else {
$endpoint = 'https://buy.itunes.apple.com/verifyReceipt';
}
$postData = json_encode(array(
'receipt-data' => $receipt,
'password' => $sharedSecret));
$ch = curl_init($endpoint);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
$response = curl_exec($ch);
$errno = curl_errno($ch);
$errmsg = curl_error($ch);
curl_close($ch);
this is as good as I can get. But not good enough.
logMessage(LogType::Info,"XXX URLFetch 0");
$postData = json_encode(array(
'receipt-data' => $receipt,
'password' => $sharedSecret));
$post_data = json_decode($postData);
logMessage(LogType::Info,"XXX URLFetch 1");
$data = http_build_query($post_data);
logMessage(LogType::Info,"XXX URLFetch 2");
$context = [
'http' => [
'method' => 'post',
'header' => "Content-Type: application/x-www-form-urlencoded\r\n",
'content' => $data
]
];
logMessage(LogType::Info,"XXX URLFetch 3");
$context = stream_context_create($context);
logMessage(LogType::Info,"XXX URLFetch 4");
$result = file_get_contents($endpoint, false, $context);
logMessage(LogType::Info,"XXX result:" . $result);
$response = $result;
$errno = 0;
logMessage(LogType::Info,"XXX response:");
It is able to post but returns this response
XXX result:{"status":21002}
Why do you json_decode $post_data in your urlfetch code but not in curl?
I had an error like this, it turned out 'POST' had to be in uppercase when running locally.
On .appspot it worked with lowercase 'post' but not locally on my PC.
When I first started, I thought Curl would be an excellent way of retrieving a chunk of data in the format json. It didn't work. I tried doing some Ajax request instead, but that didn't work either.
Now, this is my Curl request:
$ch = curl_init("url");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Accept' => 'application/json',
'Auth' => 'code',
));
$data = curl_exec($ch);
curl_close($ch);
print_r($data);
... The CURL requests RETURNS a EMPTY STRING. No errors...
$opts = array(
'http'=>array(
'method'=>"GET",
'header'=>"Accept: application/json\r\n" . "Auth: code",
)
);
$context = stream_context_create($opts);
$url = "";
$fp = fopen($url, 'r', false, $context);
$r = #stream_get_contents($fp);
fclose($fp);
print_r($r);
Provides a nice array with json data. Why? Isn't this literally supposed to do the same thing?
Because CURLOPT_HTTPHEADER doesn't take associated arrays. You need to add the complete header.
$ch = curl_init("url");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Accept: application/json',
'Auth: code',
));
$data = curl_exec($ch);
curl_close($ch);
print_r($data);