I am trying to connect to a c# rest web service by php, but when I execute the below code this error happened:
"Message":"The requested resource does not support http method 'GET'".
Could anybody help me about this error?
<?php
$url="https://example/login";
$username='user';
$password='pass';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD,$username':'$password);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$out = curl_exec($ch);
print "error:" . curl_error($ch) . "<br />";
echo $out;
curl_close($ch);
?>
By default, curl is executing the request via HTTP GET. The web service has to be accessed via POST.
Have a look at here for details on how to create a POST request via curl.
Related
I am trying to connect a website with CURL using a proxy in PHP. But I get "Received HTTP code 400 from proxy after CONNECT" error.
This is the code that I use to connect:
<?PHP
$ch = curl_init("https://ipinfo.io/json");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_PROXYTYPE, "HTTP");
curl_setopt($ch, CURLOPT_PROXY, "207.154.231.213:8080");
$output = curl_exec($ch);
if(curl_errno($ch))
echo curl_error($ch);
echo $output;
?>
I don't know what the problem is. The proxy should work perfectly because I check it.
use nex code
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, "https://ipinfo.io/json");
$result=curl_exec($ch);
curl_close($ch);
echo json_decode($result, true);
I recommend http://docs.guzzlephp.org/ library for apis for better practices
Based on the documentation curl_setopt you should either remove your CURLOPT_PROXYTYPE setting (to use the default CURLPROXY_HTTP), or you need to change your invalid setting
curl_setopt($ch, CURLOPT_PROXYTYPE, "HTTP");
to
curl_setopt($ch, CURLOPT_PROXYTYPE, "CURLPROXY_HTTP");
Note that the type HTTP does not exist.
I try to connect to socks5 proxy using libcurl, however I receive the answer SOCKS: authentication failed. I also tried to connect to the proxy server through a Powershell script with cURL – it works, so the proxy server is available and my login and password are correct. I tried a lot of solutions... But connection via PHP-script still does not work.
PHP version – 5.4, cURL version – 7.19. What can I do?
[UPDATE] Here is my current code. I want to send message with my Telegram bot :)
$proxy = '<hostname>:<port>';
$proxy_auth = '<username>:<password>';
$url = 'https://api.telegram.org/bot<botID>/sendMessage?'.'chat_id='.$chat_id.'&parse_mode='.$parse_mode.'&text='.'test';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxy_auth);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HEADER, true);
$curl_scraped_page = curl_exec($ch);
$error = curl_error($ch);
echo $error;
echo $curl_scraped_page;
curl_close($ch);
Here is the complete answer for anyone else having the same issue.
//Curl setup
$url = "https://google.com";
$proxy = "10.23.2.2:8080";
$password = "username:secret_passowrd";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
curl_setopt($ch, CURLOPT_HEADER, true);
//proxy's address
curl_setopt($ch, CURLOPT_PROXY, $proxy);
//proxy's password
curl_setopt($ch, CURLOPT_PROXYUSERPWD, $password);
//in order to follow redirects.
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
//in order to use the result of the query
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//run query
$curl_scraped_page = curl_exec($ch);
$error = curl_error($ch);
echo $error;
echo $curl_scraped_page;
curl_close($ch);
Hope that helps.
I am working with an API that provides a token for me to use to connect. It gives these instructions.
This token is then sent in all subsequent calls to the server in header variable Auth Digest.
I am not sure what this means. I've tried several approaches and read several stack overflow questions. Here's what I've tried. See code comments included for details.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,true);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
// I have tried setting both of these
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
// In combination of the above separately, I have tried each of these individually
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $token);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Auth Digest: ' . $token));
curl_setopt($ch, CURLOPT_POST, true);
$post_data = array('Auth Digest' => $token);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_data));
curl_setopt($ch, CURLOPT_USERPWD, $token);
// Then I execute and close, either giving me a failed response and a response that says token is not valid
$response = curl_exec($ch);
$header_sent = curl_getinfo($ch, CURLINFO_HEADER_OUT);
if (!$response) {
echo $action . ' curl request failed';
return false;
}
curl_close($ch);
$response_json = json_decode($response);
var_dump($response_json);
Here are some related stackoverflow questions that I've tried to apply to my problem without success.
Curl request with digest auth in PHP for download Bitbucket private repository
Client part of the Digest Authentication using PHP POST to Web Service
How do I make a request using HTTP basic authentication with PHP curl?
I need to know what the raw http header that they're likely expecting or how I can use php curl to generate the header they're likely expecting.
An digest authorization header typically looks like:
Authorization: Digest _data_here_
So in your case, try:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
//... existing options here
$headers = array(
'Authorization: Digest ' . $token,
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
If you use CURLOPT_HTTPHEADER, that just specifies additional headers to send and doesn't require you to add all the headers there.
If the other headers you're sending all have explicit options, use those and just pass the one authorization header to CURLOPT_HTTPHEADER.
i am using with curl libary to scrape web site.
when i am using proxy nothing now shown.
i took the ip proxy from hide my ass list.
here is my code. someone can tell me what is wrong ?
$url = 'http://www.sherut24.co.il';
$proxy = '189.218.145.44:10000';
$ch = curl_init(); // Initialise a cURL handle
// Setting proxy option for cURL
// Set any other cURL options that are required
curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_COOKIESESSION, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_URL, $url);
$results = curl_exec($ch); // Execute a cURL request
curl_close($ch); // Closing the cURL handle
echo $results;
I need to capture the request headers and response while using PHP cURL to post data to external API call. The localhost page load shows in traffic where as the PHP cURL is not shown.
$url = "https://https://gds.eligibleapi.com/v1.3/enrollment.json";
$ch = curl_init(); // initialize curl handle
$user_agent = $_SERVER['HTTP_USER_AGENT'];
curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
curl_setopt($ch, CURLOPT_URL, $url); // set url to post to
curl_setopt($ch, CURLOPT_FAILONERROR, 1); // Fail on errors
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // allow redirects
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // return into a variable
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string); // add POST fields
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($data_string)));
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST, 2);
$data = curl_exec($ch); // if($data === false) echo 'Curl error: ' . curl_error($ch);
echo $data;
$http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
Your URL is malformed (you have https://https://).
You need to set the Proxy option on the CURL command, e.g.
curl_setopt($ch, CURLOPT_PROXY, '127.0.0.1:8888');
See http://fiddler2.com/documentation/Configure-Fiddler/Tasks/ConfigurePHPcURL
You have to configure Fiddler to decrypt HTTPS traffic.
See here http://fiddler2.com/documentation/Configure-Fiddler/Tasks/DecryptHTTPS