Created
November 21, 2017 20:44
-
-
Save waqasraza123/cb5e38f3d260a4f42739de0261dff62c to your computer and use it in GitHub Desktop.
This file contains hidden or 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 = 'b0289g98r0geuq4e73ljrslrn1'){ | |
//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