php curl GET request with cookies - php

I am using the below code to initiate a GET request but I get only "status":403 from the JSON. The JSON needs a cookie to access. So I added the cookie. What mistake am I making here? Why do I get 403 status and can't access JSON?
$url = "URL HERE";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$headers = array(
"Cookie: ci_session=COOKIE HERE",
"Accept: application/json",
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
//for debug only!
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$resp = curl_exec($curl);
curl_close($curl);
var_dump($resp);

Related

Post request empty response PHP curl

When trying to send a post request to the server, the response returns an empty array "{}". But there is an answer on postsman. Presumably this is related to the content type of the response
<?php
$url = "https://api.business.kazanexpress.ru/api/oauth/token?
grant_type=password&username={}&password={}";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$headers = array(
"Authorization: Basic token",
"Content-Type: application/json",
"Content-Length: 0",
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
//for debug only!
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$resp = curl_exec($curl);
curl_close($curl);
var_dump($resp);
?>
Usually the OAuth2 provider will want you to send the data inside the POST body and not in the URL. To do this you can send an array with your grant_type, username and password using CURLOPT_POSTFIELDS and remove the Content-Type and Content-Length headers since cURL will generate them for you when using CURLOPT_POSTFIELDS.
It should look something like this
<?php
$url = "https://api.business.kazanexpress.ru/api/oauth/token";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, [
'username' => '',
'password' => '',
'grant_type' => 'password'
]);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$headers = [
"Authorization: Basic token",
];
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
//for debug only!
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$resp = curl_exec($curl);
curl_close($curl);
var_dump($resp);

True Caller Mobile Web SDK Integration

I am integrating Truecaller mobile web SDK in my CodeIgniter application for verification. I am successfully invoking the true caller for verification, but I am not getting the response at the endpoints. It is mentioned in the document that they post the response in a few milliseconds, but I am accessing that with the $_POST variable. Is it correct? Can anyone guide me in this, please?
if (isset($_POST["requestId"]) != 'null') {
$endpoints = $_POST["endpoint"];
log_message('error', $_POST["requestId"]);
$url = $endpoint;
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$access_token = "Bearer ".$_POST["accessToken"];
$headers = array(
"Authorization: $access_token",
"Cache-Control: no-cache",
"Content-Type: application/json",
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
//for debug only!
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$resp = curl_exec($curl);
curl_close($curl);
var_dump($resp);
}

How to Retrieve cURL Data within the URL Endpoint

I am trying to create a cURL api, let's say the below is my curl request code, where http://localhost/api/endpoint.php is my endpoint. What would actually be going on, code-wise in the endpoint.php file? How do I actually get the data that is sent via the cURL request, like the CURLOPT_USERPWD data, in the endpoint.php page? Thanks.
$ch = curl_init();
$url = 'http://localhost/api/endpoint.php';
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_USERPWD, 'alex:password123');
$headers = array(
'Accept: application/json',
'Content-Type: application/x-www-form-urlencoded',
'Accept-Language: en_US'
);
curl_setopt($ch,CURLOPT_HTTPHEADER,$headers);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$vars['grant_type'] = 'client_credentials';
$req = http_build_query($vars);
curl_setopt($ch, CURLOPT_POSTFIELDS, $req);
$response = curl_exec($ch);
You can access to these data via the $_SERVER superglobal.
Endpoint :
<?php
$user = $_SERVER['PHP_AUTH_USER']; // alex
$password = $_SERVER['PHP_AUTH_PW']; // password123
in your file endpoint.php you can print your posted value using
print_r($_POST);
after your curl print response like
print_r($response);

Post status on Google Plus using PHP Curl

I have this code for logging into Google using Simple DOM Parser with curl. I've tried to post status on google plus using following code but unable to post on google plus
Any idea on how to solve this?
Here's my code for reference:
$clientlogin_url = "https://www.google.com/accounts/ClientLogin";
$clientlogin_post = array(
"accountType" => "HOSTED_OR_GOOGLE",
"Email" => "youremail#gmail.com",
"Passwd" => "yourpassword",
"service" => "writely",
"source" => "your application name"
);
// Initialize the curl object
$curl = curl_init($clientlogin_url);
// Set some options (some for SHTTP)
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $clientlogin_post);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
// Execute
$response = curl_exec($curl);
// Get the Auth string and save it
preg_match("/Auth=([a-z0-9_-]+)/i", $response, $matches);
$auth = $matches[1];
$params['newcontent'] = "Post on Google Plus Test By Me";
$headers = array(
"Authorization: GoogleLogin auth=" . $auth,
"GData-Version: 3.0",
);
// Make the request
curl_setopt($curl, CURLOPT_URL, 'https://www.plus.google.com/');
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
$response = curl_exec($curl);
curl_close($curl);
Thanks For your help and co-operation
There is not a Google+ API to post statuses (unless you are a Google Apps user) and ClientLogin has been deprecated and will stop working soon. You're best bet is to look into using something like the share plugin.
In order not show the moved you have to set the agent header like firefox or something else
example
curl_setopt($curl, CURLOPT_URL, 'https://www.plus.google.com/');
curl_setopt($curl,"USER_AGENT","firefox my browser");
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); //Added here
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); //Added here
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); // Since you got 301 redirect
$response = curl_exec($curl);
var_dump($response);// Added here
curl_close($curl);`enter code here`
You need to add these two cURL params here too.
curl_setopt($curl, CURLOPT_URL, 'https://www.plus.google.com/');
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); //Added here
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); //Added here
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); // Since you got 301 redirect
Also, you are not outputting any response on your code
$response = curl_exec($curl);
var_dump($response);// Added here
curl_close($curl);

How do I make a PATCH request in PHP using cURL?

I have to make a PATCH request using PhP cURL. I couldn't find any documentation, so I tried the following but it isn't working.
$data = "{'field_name': 'field_value'}";
$url = "http://webservice.url";
$headers = array('X-HTTP-Method-Override: PATCH');
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($curl);
curl_close($curl);
Any idea why this isn't working? How can I fix it?
Edit:
I am connecting to a RESTful web service. It returns HTTP/1.1 200 for successful requests. Unsuccessful requests return HTTP/1.1 403. I keep getting 403.
I tried changing $data to:
$data = "data={'field_name': 'field_value'}";
It didn't change the outcome.
Edit2:
The final working code.
$data = "{'field_name': 'field_value'}";
$url = "http://webservice.url";
$headers = array('Content-Type: application/json');
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PATCH');
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($curl);
curl_close($curl);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PATCH'); should do it.
JSON PATCH would be better for data format since this format is designed for HTTP PATCH request. See https://www.rfc-editor.org/rfc/rfc6902 for the spec. The tutorial of Rails 4 show the example(http://edgeguides.rubyonrails.org/upgrading_ruby_on_rails.html#http-patch).
// https://www.rfc-editor.org/rfc/rfc6902#section-4
$data = '{ "op": "add", "path": "/a/b/c", "value": "foo" }';
$headers = array('Content-Type: application/json-patch+json');
Try Using normal array
//$data = "{'field_name': 'field_value'}";
$data = array('field_name' => 'field_value' );
You can define methods and use everything in one curl function. Hope this helps you.
define('GI_HTTP_METHOD_GET', 'GET');
define('GI_HTTP_METHOD_POST', 'POST');
define('GI_HTTP_METHOD_PUT', 'PUT');
define('GI_HTTP_METHOD_PATCH', 'PATCH');
curl_setopt($ch, CURLOPT_POST, true);
if ($method === GI_HTTP_METHOD_PUT) {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, GI_HTTP_METHOD_PUT);
}
if ($method === GI_HTTP_METHOD_PATCH) {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, GI_HTTP_METHOD_PATCH);
}

Categories