I am using curl in my application.
My function in DefaultController:
public function actionWebServices()
{
$data = array("account" => "1234", "dob" => "30051987", "site" => "mytestsite.com");
$fields='';
foreach ($data as $key => $value)
{
$fields .= $key . '/' . $value . '/';
}
rtrim($fields, '&');
$u='admin';
$p='admin123';
$url='localhost:83/Working-copy/mysite/ServiceTypeMaster/Test';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
curl_setopt($ch, CURLINFO_HEADER_OUT, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_USERPWD, $u.':'.$p);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, count($data));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
session_write_close();
$result = curl_exec($ch);
curl_close($ch);
session_start();
return $result;
}
In serviceTypeMasterController:
public function actionTest()
{
echo "test";
}
I am calling URL of localhost in curl URL. but curl doesn't give any response. It only gives me only Response Doesn't contain any data.
What is wrong?
Typically, that response is if the page/service does not exist. Are you sure you're running your server on port 83 (and not on default 80) and that the url resolves to your actionTest (instead of test) method?
Related
header('Content-type: text/html; charset=utf-8');
require 'phpQuery/phpQuery.php';
function debug($arr) {
echo '<pre>'. print_r($arr, true). '</pre>';
}
function getContent($url, $data = []) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_COOKIEJAR, __DIR__ . '/cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, __DIR__ . '/cookie.txt');
$res = curl_exec($ch);
curl_close($ch);
return $res;
}
$urlAuth = 'https://auth.mail.ru/cgi-bin/auth';
$url = 'https://wf.mail.ru/officers';
$auth_data = [
'Page' => 'https://wf.mail.ru/',
'FakeAuthPage' => 'https://wf.mail.ru/auth',
'Login' => 'MyNail',
'Password' => 'MyPass',
'do_login' => ''
];
$data = getContent($urlAuth, $auth_data);
$data = getContent($url);
debug($data);
In the cookie, the data is written fine, but when I go to the page wf.mail.ru/officers content is not displayed.
Tell me what the error is. Thank.
Added error output, error: Empty reply from server. But why is the answer empty?
I added to getContent()
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_USERAGENT, 'Googlebot/2.1 (+http://www.google.com/bot.html)');
if (!empty($data))
curl_setopt($ch, CURLOPT_POST, true);
else
curl_setopt($ch, CURLOPT_POST, false);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
This is my function:
function postCurl($url, $jsql) {
$data = array('sql' => $jsql);//jsql is a json string
$headers = array('Content-Type: application/x-www-form-urlencoded');
var_dump($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,TRUE);
$output = curl_exec($ch);
return $output;
}
It always returns false. I've tried to make the same request with REST client and it works.
Check in the server whether curl is enabled or not!!
and use the below code
$data = array('name' => $_REQUEST['name']);
$ch = curl_init('www.example.com/');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
$response = curl_exec($ch);
curl_close($ch);
$xml_output = htmlentities($response);
I'm not very good in php, but i don't understand why my code is not working it's like the function is not even launch because i receive nothing not even an error.
here is my code:
<?php
define("ZDAPIKEY", "MYTOKEN");
define("ZDUSER", "MYUSER");
$lauchnfunction = curlWrap();
/* Note: do not put a trailing slash at the end of v2 */
function curlWrap()
{
$ch = curl_init();
$url = "https://cubber.zendesk.com/api/v2/tickets/recent.json";
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10 );
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, ZDUSER."/token:".ZDAPIKEY);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
curl_setopt($ch, CURLOPT_USERAGENT, "MozillaXYZ/1.0");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$output = curl_exec($ch);
curl_close($ch);
$decoded = json_decode($output);
return $decoded;
$data = curlWrap("/tickets/recent.json", null, "GET");
var_dump($data);
}
?>
of course i replace MYTOKEN and MYUSER with my real value
I am trying to call some http web-service with php cURL. I can make the request and get successful response when I try calling that service in the browser. But whenever I try to call it via cURL, it fails with error message: couldn't connect to host
The code I am using here is as follows:
$url = 'http://myserviceurl.com';
$fields = array(
'field1' => urlencode('field1value'),
'field2' => urlencode('field2value'),
'field3' => urlencode('field3value')
);
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');
$ch = curl_init();
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
$result = curl_exec($ch);
if($result === false)
{
echo sprintf('<span>%s</span>CURL error:',curl_error($ch));
return;
}
How can I fix/diagnose this?
This could happen if you have a browser based http proxy set
Did you try setting SSL_VERIFYER to false?
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
#Bijoy(In the another answer) is correct. There was a proxy set in the browser. I changed the code to add the proxy with CURLOPT_PROXY like this:
$proxy = '<my-proxy-ip>:<proxy-port>';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
$result = curl_exec($ch);
Now it works.
I have been trying to post login credentials to an https site using cURL and PHP with no luck. Everything works fine for unsecured sites but I can't get it with https. I know the headers details that I am posting are correct (although I mocked them up for the sake of this example). Please help.
<?php
// Initialize cURL
$ch = curl_init('https://secured-example.com/auth.asp');
// Enable HTTP POST
curl_setopt($ch, CURLOPT_POST, 1);
// Use SSL
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
// Set POST parameters
curl_setopt($ch, CURLOPT_POSTFIELDS, 'username=myUser&password=myPass');
// Imitate classic browser's behavior - handle cookies
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// Execute 1st request
$store = curl_exec($ch);
// Set file to download
curl_setopt($ch, CURLOPT_URL, 'https://secured-example.com/file.pdf');
// Execute 2nd request (file download)
$content = curl_exec($ch);
curl_close($ch);
?>
Export the certificate.
Upload it to where your script can see it.
Then add:
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_CAINFO, getcwd() . "/path/to/certificateCA.crt");
I used this once to connect to a bank account, hope this helps:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, HSBC_LINK1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
$post_data = array('post1' => 'value');
$fields_string = '';
foreach($post_data as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
$fields_string = rtrim($fields_string,'&');
curl_setopt($ch, CURLOPT_POST, count($post_data));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
$data1 = curl_exec($ch);