Created
November 27, 2017 19:36
-
-
Save waqasraza123/a35219ce877a5b014b48d71d47abd999 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http\Controllers; | |
use GuzzleHttp\Client; | |
class PostsAuthController extends Controller | |
{ | |
//class variables | |
private $sessionId; | |
public $curl; | |
public function __construct() | |
{ | |
$this->curl = curl_init(); | |
} | |
/** | |
* login to the WordPress site | |
* so we can later publish the post | |
*/ | |
public function login(){ | |
curl_setopt_array($this->curl, array( | |
CURLOPT_URL => "http://trypm.org.au/api/login", | |
CURLOPT_RETURNTRANSFER => true, | |
CURLOPT_ENCODING => "", | |
CURLOPT_MAXREDIRS => 10, | |
CURLOPT_TIMEOUT => 30, | |
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, | |
CURLOPT_CUSTOMREQUEST => "POST", | |
CURLOPT_POSTFIELDS => "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"username\"\r\n\r\ntry\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"password\"\r\n\r\nWYg@(3&j#)t1QxuO@3hJiG!Q\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--", | |
CURLOPT_HTTPHEADER => array( | |
"cache-control: no-cache", | |
"content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW" | |
), | |
)); | |
$response = curl_exec($this->curl); | |
$err = curl_error($this->curl); | |
/*curl_close($this->curl);*/ | |
if ($err) { | |
echo "cURL Error #:" . $err; | |
} else { | |
$json = json_decode($response, true); | |
$this->sessionId = ($json['result'][0]['session_id']); | |
} | |
$this->createPost($this->sessionId); | |
} | |
/** | |
* create post after the login | |
* @param $sessionId | |
*/ | |
public function createPost($sessionId){ | |
//dd($sessionId); | |
curl_setopt_array($this->curl, array( | |
CURLOPT_URL => "http://trypm.org.au/api/newpost", | |
CURLOPT_RETURNTRANSFER => true, | |
CURLOPT_ENCODING => "", | |
CURLOPT_MAXREDIRS => 10, | |
CURLOPT_TIMEOUT => 30, | |
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, | |
CURLOPT_CUSTOMREQUEST => "POST", | |
CURLOPT_POSTFIELDS => "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"subject\"\r\n\r\nwaqas\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"content\"\r\n\r\nI am great.\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"thumbnail_id\"\r\n\r\n1318\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--", | |
CURLOPT_HTTPHEADER => array( | |
"cache-control: no-cache", | |
"content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW", | |
"SESSION_ID:".$sessionId | |
), | |
)); | |
$response = curl_exec($this->curl); | |
//dd($response); | |
$err = curl_error($this->curl); | |
curl_close($this->curl); | |
if ($err) { | |
echo "cURL Error #:" . $err; | |
} else { | |
echo $response; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment