Cron Authentification Digest Symfony - symfony

My website is on an Infomaniak cloud server.
I can create a cron task with Authentification Digest.
So I made a Method "updateAction()" that update my database.
If I run this method in my browser (http://site1.com/update/), then I authenticate with my username and password. (It works and the database is updated)
I create a cronjob in my "Infomaniak Cloud Server" with Digest Authentication and the URL : http://site1.com/update/
I obtain this mail notification :
Cron name : Update
Date : 19/01/2018 16:51:18
Url : https://site1.com/update/
Result : OK
Detail : 204
Reponse : No response
So, the HTTP response is "204 : No content"
And my database is not updated.
Could you help me ?
So the solution is :
$url="http://site1.com/update/";
define('USERNAME','user');
define('PASSWORD','12345');
$ch=null;
try
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, USERNAME . ":" . PASSWORD);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
$resp = curl_exec($ch);
}
catch(Exception $ex)
{
}

Related

Make batch http request from wordpress plugin using wp_remote_post

I'm trying to make batch request from a wordpress plugin to the gmail API to download multiple messages at once. I can get them using curl function:
$BOUNDARY='gmail-message-boundary';
$body= "";
foreach ($message_ids as $message_id) {
$body.= "--$BOUNDARY\n";
$body.= "Content-Type: application/http\n\n";
$body.= 'GET https://www.googleapis.com/gmail/v1/users/'.$email_id.
'/messages/'.$message_id->id.'?metadataHeaders=From&format=metadata&key='.urlencode($gmail_api_key)."\n\n";
$post_body .= "--$BOUNDARY--\n";
$headers = [
'Content-type: multipart/mixed; boundary='.$BOUNDARY,
'Authorization: OAuth '.$auth_token
];
$curl = curl_init();
curl_setopt($curl,CURLOPT_URL, 'https://www.googleapis.com/batch/gmail/v1' );
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl,CURLOPT_CONNECTTIMEOUT , 60 ) ;
curl_setopt($curl, CURLOPT_TIMEOUT, 60 ) ;
curl_setopt($curl,CURLOPT_POSTFIELDS , $body);
curl_setopt($curl, CURLOPT_RETURNTRANSFER,TRUE);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER,0);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
$res= curl_exec($curl);
curl_close($curl);
this code works perfectly but I want to achieve the same result using only worpdress functions. Unfortunately, when I'm trying to make the request using wp_remote_post function I'm getting this response:
{
"headers":{
},
"body":"{\n \"error\": {\n \"code\": 400,\n \"message\": \"Failed to get multipart boundary.\",\n \"status\": \"INVALID_ARGUMENT\"\n }\n}\n",
"response":{
"code":400,
"message":"Bad Request"
},
"cookies":[
],
"filename":null,
"http_response":{
"data":null,
"headers":null,
"status":null
}
}
Does anyone knows how to use wordpress functions to make batch http request?

Curl / Guzzle - get header / response code without body

To get the statuscode of a website with curl you can use the CURLOPT NOBODY.
Example:
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'http://www.example.com');
curl_setopt($curl , CURLOPT_NOBODY, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$status = curl_exec($curl);
curl_close($curl);
Is the following example with Guzzle as http library the same:
$guzzle = new Client();
$req = $guzzle->createRequest('GET', 'http://www.example.com');
$result = $guzzle->send($req);
$status = $result->getStatusCode();
My goal is to perform a curl/guzzle request without getting the body. Will that request with Guzzle only fetch the status code without wasting bandwith on other data?
In order to get status code of the response without downloading the whole content, you should use "head" method:
$client = new \GuzzleHttp\Client();
$response = $client->head('http://example.com/');
echo $response->getStatusCode();

Alfresco login page bypassing

I created a valid ticket using a webservice call...code shown below
$url="http://serverip:port/alfresco/service/api/login?u=xxx&pw=xxx";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
$response =curl_exec($ch);
Now using this ticket i want to authenticate alfresco without again entering username and password.Also i want to create a valid cookie JSESSIONID inside browser with this ticket...Is it feasible??
my purpose is to integrate a php application with alfresco....php application already has an authentication system...so i want to bypass the authentication of alfresco
You need to append below parameter
alf_ticket="TICKET_WHICH_YOU_GET"
for further authentication.
Finally i resolved the issue by calling the login page url http://ip:port/share/page/ via Curl with login parameters(username and pwd)...I got JsessionId as response from curl...Now i took that JsessionId and set inside the browser...so wen u click http://ip:port/share/page/ the login page is bypassed
As per your suggestion, we are tried with below curl call but their is no JsessionId in response. can you please check and let me know the resolution
$post = [
'username' => 'user',
'password' => 'pass',
];
$ch = curl_init('http://ip:port/share/page/dologin/');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
// execute!
$response = curl_exec($ch);
// close the connection, release resources used
curl_close($ch);
// do anything you want with your response
var_dump($response);
please suggest with the solution

scraping a website eith POST method

Hi im scraping a website cleartrip.com
I get the page info by this method :
$url = "http://www.cleartrip.com/m/flights/results?from=CCU&to=DEL&depart_date=22/06/2012&adults=1&childs=0&infants=0&dep_time=0&class=Economy&airline=&carrier=&x=57&y=16&flexi_search=no&tb=n";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, "android Mozilla/5.0 (Linux; U; Android 0.5; en-us)");
$curl_scraped_page = curl_exec($ch);
curl_close($ch);
Problem is i want those flights that have a stop ..............info is passed through post method but i don't know how i can get it?????
You'll probably need to search the DOM for the element containing the info you want. Probably through a regular expression.

200 HTTP CODE at non-url strings

$agent = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)";
$ch=curl_init();
curl_setopt ($ch, CURLOPT_URL,$url );
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch,CURLOPT_VERBOSE,false);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch,CURLOPT_SSLVERSION,3);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST, FALSE);
$page=curl_exec($ch);
//echo curl_error($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
I'm using cUrl at my project. I'm getting HTTP CODE of web sites. But when i try "asdasd" ($url="asdasd") , returned HTTP CODE is 200 . But "asdasd" isn't a web site. Why HTTP CODE is 200 ?
You might want to check the return value from curl_exec first. It says in the manual:
Returns TRUE on success or FALSE on failure. However, if the CURLOPT_RETURNTRANSFER option is set, it will return the result on success, FALSE on failure.
For curl_getinfo there's a side note:
Information gathered by this function is kept if the handle is re-used. This means that unless a statistic is overridden internally by this function, the previous info is returned.
So that HTTP 200 might well be the result of a previous cURL invocation.
Another possible error can happen if you develop in local and you use custom managed DNS services, then you probably can get some managed error pages like OpenDNS is doing.

Resources