简体   繁体   中英

how can i get parase data from another website using php

I want to get decode data from server to server server sender sends echo text like title decode to the receiver server I use json_decode($json ,tue); and $json = file_get_contents($url ,true); not working

it is show the title in the sender server but does not get in the receiver server

parse domain sender includes this code

<?php
$json = '{
    "title": "PHP"

}';
$data = json_decode($json);
echo $data->title;
?>

the second domain receiver from the domain sender

<?php

$url = 'https://domain sender';
$json = file_get_contents($url);
$jo = json_decode($json);
echo $jo->title;


if ($jo = 'PHP') {

    $sql = "UPDATE users SET web_status = 'web_int' WHERE website = '".$url."' ";

    if ($conn->query($sql) === true) {
    }
    echo"good";
}else{
    if ($json !== 'PHP') {

        $sql = "UPDATE users SET web_status = 'pendding' WHERE website = '".$url."' ";

        if ($conn->query($sql) === TRUE) {
        }
        echo"bad";
        $conn->close();
    }
}
?>

Your sender code will output only the title value, which is "PHP".

Therefore, there is no need for the receiver to try and decode any JSON, because the data it downloads is simply one word, in plain text format.

There are also a couple of other simplifications and improvements we can make to the receiver code, so that we end up with this:

$url = 'https://domain sender';
$data = file_get_contents($url); //downloaded data is plain text, not JSON
$status = "";

// a simple switch is usually neater than a lot of "if"s
switch ($data) {
  case "PHP":
    $status = "web_int";
    echo "good";
    break;
  default:
    $status = "pending";
    echo "bad";
    break;
}

//always use prepared statements and parameters to execute queries reliably and safely!
$sql = "UPDATE users SET web_status = ? WHERE website = ? ";
$stmt = $conn->prepare($sql);
$stmt->execute(array($status, $url));

On sending server you have echo $data->title; which echo/sends 'PHP'

Then you have $jo = json_decode($json); echo $jo->title; $jo = json_decode($json); echo $jo->title; on receiving server. But the ->title isn't needed because you don't send an array. The variable $jo already has the value 'PHP'.

Update:

Change

$url = 'https://domain sender';
$json = file_get_contents($url);
$jo = json_decode($json);
echo $jo->title;

To

$url = 'https://domain sender';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
curl_close($ch);
echo $output;

Tested it and it works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM